1. 程式人生 > >百度圖片http://img[0-9]\.imgtn.*?g此形式的連結圖片下載方式

百度圖片http://img[0-9]\.imgtn.*?g此形式的連結圖片下載方式

"""給出圖片連結列表, 下載圖片"""
print(pic_urls)
for pic_url in pic_urls:
    try:
        host = get_url_host(pic_url)
        headers["Host"] = host
        req = urllib.request.Request(pic_url, headers=headers)
        pic = urllib.request.urlopen(req, timeout=20)
        with open(localPath + '%d.jpg' % count, 'wb')as f:
f.write(pic.read()) print('成功下載第%s張圖片: %s' % (str(count + 1), str(pic_url))) except Exception as e: print('下載第%s張圖片時失敗: %s' % (str(count + 1), str(pic_url))) print(e) continue count = count + 1 def get_url_host(url):######http://img[0-9]\.imgtn.*?
檔案無法下載,需要進行設定 reg = r'http://(.*?)/' hostre = re.compile(reg) host = re.findall(hostre, url) if len(host) > 0: return host[0] return ""