百度图片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 ""
原文地址:https://www.cnblogs.com/cekong/p/9968753.html