zoukankan      html  css  js  c++  java
  • 百度图片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 ""
  • 相关阅读:
    python 代码片段8
    python 代码片段7
    python 代码片段6
    python 代码片段5
    python 代码片段4
    django 代码片段3
    python 代码片段2
    Redis事物
    Redis的java客户端jedis
    Redis五大数据类型
  • 原文地址:https://www.cnblogs.com/cekong/p/9968753.html
Copyright © 2011-2022 走看看