python – 即使在收到200状态代码时重试Scrapy请求
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 即使在收到200状态代码时重试Scrapy请求,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1514字,纯文字阅读大概需要3分钟。
内容图文
![python – 即使在收到200状态代码时重试Scrapy请求](/upload/InfoBanner/zyjiaocheng/761/98e9f8764fa94787a5e9efdab1a8d53d.jpg)
有一个我正在抓取的网站,有时会返回200,但在response.body中没有任何文本(当我尝试用Selector解析时引发一个AttributeError).
是否有一种简单的方法来检查以确保正文包含文本,如果没有,请重试请求,直到它为止?这是一些伪代码来概述我正在尝试做什么.
def check_response(response):
if response.body != '':
return response
else:
return Request(copy_of_response.request,
callback=check_response)
基本上,有没有办法可以使用完全相同的属性(方法,URL,有效负载,cookie等)重复请求?
解决方法:
Easier to ask for forgiveness than permission. This common Python
coding style assumes the existence of valid keys or attributes and
catches exceptions if the assumption proves false. This clean and fast
style is characterized by the presence of many try and except
statements. The technique contrasts with the LBYL style common to many
other languages such as C.
处理异常并使用dont_filter=True向当前URL发出请求:
dont_filter (boolean) – indicates that this request should not be
filtered by the scheduler. This is used when you want to perform an
identical request multiple times, to ignore the duplicates filter. Use
it with care, or you will get into crawling loops. Default to False.
def parse(response):
try:
# parsing logic here
except AttributeError:
yield Request(response.url, callback=self.parse, dont_filter=True)
您还可以在make a copy当前请求(未测试):
new_request = response.request.copy()
new_request.dont_filter = True
yield new_request
或者,使用replace()发出新请求:
new_request = response.request.replace(dont_filter=True)
yield new_request
内容总结
以上是互联网集市为您收集整理的python – 即使在收到200状态代码时重试Scrapy请求全部内容,希望文章能够帮你解决python – 即使在收到200状态代码时重试Scrapy请求所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。