V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
Langjan
V2EX  ›  Python

爬图片下载的都是同一张假图片,在浏览器直接打开图片地址显示的也不是原图

  •  
  •   Langjan · 2018-08-29 20:08:31 +08:00 · 3137 次点击
    这是一个创建于 2037 天前的主题,其中的信息可能已经有所发展或是发生改变。
    获取到 http://www.zhuoku.com 网站的图片地址,下载回来的都是同一张假图片,在浏览器直接打开图片地址显示的也不是原图。请教各位大佬这个问题如何解决。
    第 1 条附言  ·  2018-08-30 14:34:49 +08:00
    已解决,原来是 headers 信息多了也不行

    import requests

    i_headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
    'Referer':'http://www.zhuoku.com/zhuomianbizhi/jing-car/20180425160320(1).htm',
    }
    jpg_url = 'http://bizhi.zhuoku.com/2018/04/25/Bugatti/2019-Bugatti-Chiron-Sport-01.jpg'
    content = requests.get(jpg_url, headers = i_headers).content
    with open('F:\\PyDowns\\zhuoku\\demo.jpg', 'wb') as fp:
    fp.write(content)
    xiaocsl
        1
    xiaocsl  
       2018-08-29 21:08:59 +08:00
    如果用的不是同一套 cookies 的话,那就是封 IP 了,上代理池吧.
    delectate
        2
    delectate  
       2018-08-29 21:43:41 +08:00
    通常就是 cookies、refer url、IP,逐个排查。
    einverne
        3
    einverne  
       2018-08-30 08:37:20 +08:00
    大概是因为给识别出机器人网站给你返回了错误的图片,换个 IP 试试
    simoncc
        4
    simoncc  
       2018-08-30 09:11:44 +08:00
    应该是缺了 refer 头,在 headers 中加上。
    imdong
        5
    imdong  
       2018-08-30 09:12:01 +08:00
    加上来路就好了,常见的图片防盗链。
    Referer
    Langjan
        6
    Langjan  
    OP
       2018-08-30 11:32:10 +08:00
    图片地址 http://bizhi.zhuoku.com/2018/04/25/Bugatti/2019-Bugatti-Chiron-Sport-10.jpg
    在这个下 http://www.zhuoku.com/zhuomianbizhi/jing-car/20180425160320(10).htm
    htm 可以直接访问不用加 referer,在该页面显示图片正常,图片右击新标签页打开也正常
    (浏览器打开正常应该不是封 IP 吧)

    代码如下返回了错误的图片
    import urllib.request
    import requests
    import re

    headers = {
    'Host':'www.zhuoku.com',
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
    'Referer':'http://www.zhuoku.com/zhuomianbizhi/jing-car/20180425160320(10).htm'
    'Cookie':'cck_lasttime=1535598582789; cck_count=0; bdshare_firstime=1535598583191'
    }
    url = 'http://www.zhuoku.com/zhuomianbizhi/jing-car/20180425160320(10).htm'
    req = requests.get(url, headers = headers)
    req.encoding = 'GBK'
    html = req.text
    picurl = re.findall(r'<img id="imageview" src="(.*?)"', html)[0]
    picname = re.findall(r'thumbs/tn_(.*?)"', html)[0]
    path = 'F:\\PyDowns\\zhuoku\\' + picname
    urllib.request.urlretrieve(picurl, path)
    Langjan
        7
    Langjan  
    OP
       2018-08-30 11:41:29 +08:00
    打开 20180425160320(10)
    request headers 为
    GET 20180425160320(10) HTTP/1.1
    Host: com
    Proxy-Connection: keep-alive
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6

    其中图片的 request headers 为
    GET 2019-Bugatti-Chiron-Sport-10 HTTP/1.1
    Host: com
    Proxy-Connection: keep-alive
    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    Accept: image/webp,image/apng,image/*,*/*;q=0.8
    Referer: 20180425160320(10)
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6
    (由于论坛回复机制删除了 URL )
    Langjan
        8
    Langjan  
    OP
       2018-08-30 14:22:19 +08:00
    urllib.request.urlretrieve() 函数不能提交 header 信息?
    下载图片时(等同直接访问图片地址)就会触发防盗,返回错误的图片

    这个方法也是不行
    content = requests.get(picurl, headers =headers).content
    with open('F:\\PyDowns\\zhuoku\\demo.jpg', 'wb') as fp:
    fp.write(content)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5413 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 08:47 · PVG 16:47 · LAX 01:47 · JFK 04:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.