V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Sep 28, 2017 · 2782 views
    This topic created in 3132 days ago, the information mentioned may be changed or developed.

    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 replies    2017-10-01 10:56:11 +08:00
    anywalker707
        1
    anywalker707  
    OP
       Sep 28, 2017
    这个问题不难吧。。。
    onlyice
        2
    onlyice  
       Oct 1, 2017 via Android
    301 为啥就下载失败? Scrapy 应该会自动跳转 301 吧。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   941 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 22:39 · PVG 06:39 · LAX 15:39 · JFK 18:39
    ♥ Do have faith in what you're doing.