python - Scrapy 发出请求后不返回

标签 python web-scraping scrapy

类似于这里的人:Scrapy Not Returning Additonal Info from Scraped Link in Item via Request Callback ,我无法访问我在回调函数中构建的项目列表。我尝试在解析函数(但不起作用,因为回调未返回)和回调中构建列表,但两者都对我不起作用。

我正在尝试返回我根据这些请求构建的所有项目。我在哪里调用“返回”以便该商品已得到完全处理?我正在尝试复制该教程( http://doc.scrapy.org/en/latest/intro/tutorial.html#using-our-item ) 谢谢!!

相关代码如下:

class ASpider(Spider):
    items = []
...
  def parse(self, response):
      input_file = csv.DictReader(open("AFile.txt"))
      x = 0
      for row in input_file:
          yield  Request("ARequest", 
          cookies = {"arg1":"1", "arg2":row["arg2"], "style":"default", "arg3":row["arg3"]}, callback = self.aftersubmit, dont_filter = True)

  def aftersubmit(self, response):
    hxs = Selector(response)
    # Create new object..
    item = AnItem()
    item['Name'] = "jsc"
    return item

最佳答案

您需要从 aftersubmit 回调方法中返回(或产生)一个项目。引用自docs :

In the callback function, you parse the response (web page) and return either Item objects, Request objects, or an iterable of both.

def aftersubmit(self, response):
    hxs = Selector(response)

    item = AnItem()
    item['Name'] = "jsc"
    return item

请注意,这个特定的 Item 实例没有意义,因为您还没有真正将响应中的任何内容放入其字段中。

关于python - Scrapy 发出请求后不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23876269/

相关文章:

python - 为python程序生成突变体

python - 无法使用 set 剔除重复结果

python - 使用 scrapy 提取具有特定 css 类的链接

python - 如何使用scrapy获取XMLHTTP请求的数据

python - IO错误: [Errno 13] Permission denied: I do have permissions?

python - 遍历python中的对象属性

python - 在 Python 中创建 CLI(Shell?)

php - 从 PHP 页面中抓取 Price Div 类

r - 在 Yahoo! 中抓取关键统计数据用 R 理财

python - 使用 Win10 任务调度程序批量调度 Scrapy Spider