【问题标题】:How to receive the response of scrapy.Request() in the same function it is called from?如何在调用它的同一函数中接收 scrapy.Request() 的响应?
【发布时间】:2020-06-06 17:03:39
【问题描述】:

我对 Scrapy 很陌生,并且有这个要求,我需要将 Scrapy 请求的响应返回到调用它的函数。 目前我通过使用scrapy-inline-requests 库只找到了一种解决方案

在 Scrapy 中是否有任何原生方式?

举例

def parse(self, response):
    item = spiderItem()

    # Extract some items here from this response using CSS Selectors
    # ....
    # ....

    # Now extract URL from the response
    new_url = response.css("div.urls::text").get()
    yield scrapy.Request(new_url, callback=self.parse_more)

    # Receive the response from parse_more() here. Is it possible?
    resp = 

def parse_more(self, response):
    # This function should be able to return the response back to the parse() function for further processing.

类似于我们在 requests 库中可以做的事情

response = requests.get(url)

【问题讨论】:

    标签: web-scraping scrapy


    【解决方案1】:

    由于 Scrapy 是在 Twisted 异步库之上构建的,因此认为这是不可能的。使用 HTTP 响应调用回调方法,而不会阻塞调用线程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      相关资源
      最近更新 更多