推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
mokeyjay
V2EX  ›  Python

Python3 用 urllib 下载图片非常慢,会是什么原因呢?

  •  
  •   mokeyjay ·
    mokeyjay · Feb 27, 2017 · 6238 views
    This topic created in 3393 days ago, the information mentioned may be changed or developed.

    初学者想学写个爬虫,边学边写

    想要下载一张 Y 站的图片,代码为

    urllib.request.urlopen('http://xxx.jpg').read()
    

    其中 url 是可以正常访问的。图片不大,浏览器打开只需要几秒(排除缓存原因)。但在 python 中下载它却需要 30+秒,将下载到的数据写出为文件是可以正常查看的

    那么问题来了,究竟是什么原因导致下载一张图片那么慢呢?

    请问是还有什么地方需要配置吗?

    附完整代码:

    # 创建目录存放今天爬下来的图
    dir_name = datetime.datetime.now().strftime('%Y%m%d')
    if not os.path.exists(dir_name):
        os.mkdir(dir_name)
        
    # info[1] 的值为 https://files.yande.re/sample/6718a8caa71a4547a417f41bc9f063bb/yande.re%20385001%20sample%20byakuya_reki%20seifuku.jpg
    print('开始下载……')
    print(info[1])
    i = time.time()
    img = urllib.request.urlopen(info[1]).read()
    print('下载完毕。耗时:'+str(int(time.time() - i))+'s')
    
    # 获取文件名,并将%20 替换为空格
    file_name = info[1].split('/')[-1].replace('%20', ' ')
    file = open(dir_name+'/'+file_name, 'wb')
    file.write(img)
    file.close()
    exit(200)
    
    Supplement 1  ·  Feb 27, 2017
    经测试,是网站对爬虫限速了
    加上 UA 、 Host 、 Referer 等头信息后一切正常, XD 谢谢各位
    17 replies    2017-02-28 23:02:07 +08:00
    zhanglintc
        1
    zhanglintc  
       Feb 27, 2017
    我打开这张图也要很久
    mokeyjay
        2
    mokeyjay  
    OP
       Feb 27, 2017 via Android
    @zhanglintc 可能是区域问题?但我浏览器打开很快呀,换个浏览器速度也差不多。我的代码是在本地运行的,怎么速度差那么多呢?
    ltux
        3
    ltux  
       Feb 27, 2017
    也许服务器对爬虫限速了呢
    CloudnuY
        4
    CloudnuY  
       Feb 27, 2017
    你看看是不是打开了系统代理
    HFcbyqP0iVO5KM05
        5
    HFcbyqP0iVO5KM05  
       Feb 27, 2017 via Android
    浏览器有缓存吧
    mokeyjay
        6
    mokeyjay  
    OP
       Feb 27, 2017 via Android
    @ltux 还真有这个可能……@CloudnuY 用的是 ss 的 pac 模式,这个不碍事吧?@gulu 换过浏览器一样几秒打开
    fengxiang
        7
    fengxiang  
       Feb 27, 2017
    因为你浏览器走代理了,yandex 大一点的图片 2 30m,慢很正常
    fbtfonfp
        8
    fbtfonfp  
       Feb 27, 2017
    跑了一下 , 3s
    mokeyjay
        9
    mokeyjay  
    OP
       Feb 27, 2017
    @CloudnuY #4 用的是 ss 的 pac 模式,这个不碍事吧?
    @gulu #5 换过浏览器一样几秒打开
    @fengxiang #7 用的是 ss 的 pac 模式,这个不碍事吧?
    @fbtfonfp #8 就用上面的代码跑? 3s ?
    mianju
        10
    mianju  
       Feb 27, 2017
    如果挂 ss 的话,推荐使用这个库 PySocks
    具体使用参考 http://stackoverflow.com/questions/31777692/python3-requests-with-sock5-proxy
    mokeyjay
        11
    mokeyjay  
    OP
       Feb 27, 2017
    @mianju #10 看起来不错。不过我正是初学,希望尽量以原生的方式实现,先收藏了
    dsg001
        12
    dsg001  
       Feb 27, 2017
    一般调用 wget 下载
    fengxiang
        13
    fengxiang  
       Feb 27, 2017
    还是挂个代理吧,萌妹的服务器时不时抽风,不太好判断.
    推荐用 requests 代替 urllib
    binux
        14
    binux  
       Feb 27, 2017
    根据你的表述,就是系统代理没跑了。
    TTwilight
        15
    TTwilight  
       Feb 28, 2017
    我一般用 urlretrieve
    fbtfonfp
        16
    fbtfonfp  
       Feb 28, 2017
    @mokeyjay en 跑了上面的代码是 3s 不知道是不是因为挂了代理
    hihihihihi
        17
    hihihihihi  
       Feb 28, 2017
    1 loop, best of 3: 2.98 s per loop
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2838 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 04:31 · PVG 12:31 · LAX 21:31 · JFK 00:31
    ♥ Do have faith in what you're doing.