zoukankan      html  css  js  c++  java
  • python批量下载

    以下载图片为例:

    from gevent import monkey
    
    monkey.patch_all()
    from gevent.pool import Pool
    from urllib.parse import urlparse
    import requests
    import sys
    import os
    
    
    def download(url):
        chrome = 'Mozilla/5.0 (X11; Linux i86_64) AppleWebKit/537.36 ' + '(KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
        headers = {'User-Agent': chrome}
        r = requests.get(url.strip(), headers=headers, stream=True)
    
        output_dir = "pic"  # 图片存放文件夹
        # 判断输出文件夹是否存在。不存在就创建
        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)
    
        url_parse = urlparse(url)  # 图片来源url
        fname = os.path.split(url_parse.path)[1]  # 图片名
        filename = output_dir + os.sep + fname  # 图片存放位置
        print("开始下载:%s" % fname)
        with open(filename, 'wb')as f:
            f.write(r.content)  # 保存图片
            print("下载完成:%s" % fname)
    
    
    if __name__ == "__main__":
        if len(sys.argv) == 2:
            filename = sys.argv[1]
            f = open(filename, "r")
            p = Pool(100)
            for line in f.readlines():
                if line:
                    p.spawn(download, line.strip())
                    f.close()
                    p.join()
        else:
            print('Usage: python %s urls.txt' % sys.argv[0])
    

      

    python  downloader.py test.txt

  • 相关阅读:
    2019-12-18
    java读取XML文件,及封装XML字符串
    java不用中间变量交换两个值
    oracle获取当前月的第一个星期五
    2019-10-23
    HTTP中GET请求与POST请求的区别
    Java面试题整理(转载)
    java 字节流与字符流的区别
    20190822
    C++标准库string
  • 原文地址:https://www.cnblogs.com/andy9468/p/10671215.html
Copyright © 2011-2022 走看看