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

scrapy 的 request 的相关问题

  •  
  •   moxiaowei · Nov 16, 2018 · 2489 views
    This topic created in 2725 days ago, the information mentioned may be changed or developed.

    现在我有一个爬虫,parse 到了一个页面的相关数据,但是,这个页面的有些数据需要结合 parse 中的另一个 request 的结果才能确定是否有用,类似下面的代码:

        def parse(self, response):
    
                ...........
    
                yield scrapy.Request(xxxxx, callbask=func)问题就在于 我怎么拿到这个 request 的返回值!!!!
    
                .......
    
    6 replies    2018-11-17 14:54:04 +08:00
    nicevar
        1
    nicevar  
       Nov 16, 2018
    你不会连回调的意思都不懂吧,你那个单词都写错了不是 callbask,是 callback,在这个函数里等着就行了
    xzc19970719
        2
    xzc19970719  
       Nov 16, 2018 via Android
    callback 你用来干啥了????
    Trim21
        3
    Trim21  
       Nov 16, 2018 via Android
    callback 的 response 参数就是这个请求的响应
    你应该在 callback 参数对应的 func 里面处理这个请求对应的后续工作
    lzhd24
        4
    lzhd24  
       Nov 16, 2018
    ```
    def parse(self, response):
    urls = ["https://www.test.com/page/{}/".format(i) for i in range(1,250)]
    for url in urls:
    yield Request(url,callback=self.parse2)

    def parse2(self,response):
    article_urls = response.xpath("//div[@class='col-sm-12 col-lg-7 col-md-7']//div//a//@href").extract()
    for url in article_urls:
    yield Request(url,callback=self.parse3)

    def parse3(self,response):
    items = XiazaiItem()
    items['title']=response.xpath("//article//div//h1[@class='h2']/text()").extract()
    items['content']=response.xpath("//div[@class='article__body']//p/text()").extract()
    yield items
    ```
    chengxiao
        5
    chengxiao  
       Nov 17, 2018
    这种情况我一般就不用 yield 了
    import requests
    直接用 requests 走起
    moxiaowei
        6
    moxiaowei  
    OP
       Nov 17, 2018
    @nicevar 再这个 callback,处理了,然后上面那个 parse 怎么拿到 callback 处理的结果?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2285 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 15:54 · PVG 23:54 · LAX 08:54 · JFK 11:54
    ♥ Do have faith in what you're doing.