【问题标题】:How to use python requests with scrapy?如何将 python 请求与 scrapy 一起使用?
【发布时间】:2019-05-21 03:50:22
【问题描述】:

我正在尝试使用requests 获取页面,然后将响应对象传递给解析器,但遇到了问题:

def start_requests(self):
    yield self.parse(requests.get(url))
def parse(self, response):
  #pass

builtins.AttributeError: 'generator' 对象没有属性 'dont_filter'

【问题讨论】:

  • 我无法从您的代码中看出request 的定义位置。
  • 我认为“如果不是 request.dont_filter 和 self.df.request_seen(request):”是错误的一部分
  • 您为什么要尝试使用requests 库而不是scrapy.Requests?
  • 出于某种原因在某些网站上,请求有效
  • 您找到解决方法了吗?

标签: scrapy scrapy-shell


【解决方案1】:

您首先需要下载页面的响应,然后将该字符串转换为 HtmlResponse 对象

from scrapy.http import HtmlResponse
resp = requests.get(url)

response = HtmlResponse(url="", body=resp.text, encoding='utf-8')

【讨论】:

  • 嘿,我和 op 有同样的问题。我试过你的代码||||||||||| response = HtmlResponse(url="", body=requests.get(line).text, encoding='utf-8') 产生响应 ||||||||收到此错误 """'HtmlResponse' 对象没有属性 'dont_filter'"""
猜你喜欢
  • 2016-09-01
  • 2021-01-11
  • 2023-03-23
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 2015-06-28
  • 1970-01-01
相关资源
最近更新 更多