仅在爬行时仅抓取400误差,而不是在使用scrapy shell时
我正在收到400 HTTP状态代码,或者使用scrapy Crawl功能在 https://www.bbc.com/news/topics/c3np65e0jq4t 。我正在使用以下代码并命令来启动疤痕。
class bbc_url_spider(scrapy.Spider):
name = 'bbc_url_spider'
start_urls = ['https://www.bbc.co.uk/news/topics/c3np65e0jq4ts'
]
user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
def parse(self, response):
for url in set(response.css('a.ssrcss-1j8v9o5-PromoLink.e1f5wbog0::attr(href)').getall()):
yield{
'url': url
}
next_button = response.xpath('.//div[contains(@class,"e1b2sq420")]')[-1]
next_page_link = next_button.css('a::attr(href)').get()
if next_page_link is not None:
yield response.follow('https://www.bbc.co.uk/news/topics/c3np65e0jq4t' + next_page_link, callback=self.parse)
scrapy crawl bbc_url_spider -O bbc_urls.json
返回此日志。
但是,当使用scrapy shell时,我能够使用简单的fetch访问完全相同的网页。
不确定为什么会发生这种情况。我尝试使用不同的用户代理和中间件,但似乎无效。任何建议将不胜感激。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
中的状态代码400
和500
范围是错误,因此零工忽略了这些错误。如果您有一个特定的情况,您仍然想调用回调方法,例如这些状态代码的
Parse
,则可以通过在蜘蛛类中添加此来做到这一点:在大多数情况下,
400
将是一个错误。如果需要,可以使用errback
来处理这些错误。参见 docs 有关详细信息。Status code in
400
and500
range are errors and thus Scrapy ignores these by design.If you have a specific case where you want to still call the callback methods such as
parse
for these status codes, you can do so by adding this in your Spider class:In most cases,
400
would be an error. If you want, you can useerrback
to handle these errors. See docs for details.