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
anywalker707
V2EX  ›  Python

Scrapy 使用 ImagePiple 下载图片时,图片地址需要做一次 301 跳转,这导致下载图片失败,是否可以在下载中间件里修复,网上没有搜到解决办法

  •  
  •   anywalker707 · 2017-09-28 08:37:08 +08:00 · 1920 次点击
    这是一个创建于 2403 天前的主题,其中的信息可能已经有所发展或是发生改变。

    RT,301 跳转都一个 CDN 的地址,我目前的做法是在 pipeline 中手动加入一个替换,可以完成下载:

    def get_media_requests(self, item, info):
        for img_url in item['imageUrlList']:
            referer = item['imageTitleUrl']
            img_url = img_url.replace('oldURL', 'new_CDNURL')
    
            yield Request(img_url, meta = {'item': item,
                                           'referer': referer})
    

    但总感觉这样做有风险,因为 CDN 的 url 可能是变化的,我这里写死了,求指点合适的解决办法

    2 条回复    2017-10-01 10:56:11 +08:00
    anywalker707
        1
    anywalker707  
    OP
       2017-09-28 20:49:59 +08:00
    这个问题不难吧。。。
    onlyice
        2
    onlyice  
       2017-10-01 10:56:11 +08:00 via Android
    301 为啥就下载失败? Scrapy 应该会自动跳转 301 吧。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2961 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 12:53 · PVG 20:53 · LAX 05:53 · JFK 08:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.