cekong

导航

百度图片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 ""

 

posted on 2018-11-16 13:18  cekong  阅读(610)  评论(0编辑  收藏  举报