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

Scrapy 如何得到原始的 start_url

  •  
  •   donglongtu · 2017-06-28 22:38:05 +08:00 · 1976 次点击
    这是一个创建于 2492 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Scrapy爬虫时,由于重定向或是其他原因,会导致原始的start_url发生改变,怎样才能得到原始的start_url?

    def start_requests(self):
        start_url = 'your_scrapy_start_url'
        yield Request(start_url, self.parse)
        
    def parse(self, response):
        item = YourItem()
        item['start_url'] = 原始请求的 start_url
        yield item
    
    revotu
        1
    revotu  
       2017-06-28 22:39:34 +08:00
    Scrapy 爬虫常见问题总结 : http://www.revotu.com/scrapy-reptile-faq.html

    利用 Request 中的 meta 参数传递信息

    def start_requests(self):
    start_url = 'your_scrapy_start_url'
    yield Request(start_url, self.parse, meta={'start_url':start_url})

    def parse(self, response):
    item = YourItem()
    item['start_url'] = response.meta['start_url']
    yield item
    knightdf
        2
    knightdf  
       2017-06-29 00:20:59 +08:00
    response.request.url
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1259 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 23:21 · PVG 07:21 · LAX 16:21 · JFK 19:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.