【问题标题】:Python Spider ConnectionError: ('Connection aborted.', BadStatusLine("''",))Python Spider ConnectionError: ('Connection aborted.', BadStatusLine("''",))
【发布时间】:2018-11-18 16:06:06
【问题描述】:

我正在使用 python-scrapy 执行爬取过程。在抓取到下一页(通常在 400-500 之间)的随机迭代次数后,蜘蛛失败,引发 ConnectionError: ('Connection aborted.', BadStatusLine("''",))

我尝试了几种解决方案,例如使用带有随机用户代理的标头,并且没有其他帖子中推荐的引用者或代理,但蜘蛛一直失败。

引发的错误是:

018-06-08 16:05:34 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www... > (referer: None)
Traceback (most recent call last):
...

ConnectionError: ('Connection aborted.', BadStatusLine("''",))
2018-06-08 16:05:34 [scrapy.core.engine] INFO: Closing spider (finished)

迭代代码部分如下所示:

if self.counter <= self.limit:

            ua = UserAgent(cache=False)
            User_agent = ua.random
            headers =  {
            'User-Agent': User_agent,
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, sdch',
            'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
            'Referer':response.request.headers.get('Referrer', None),
            'Content-Type': 'application/x-www-form-urlencoded'
            }

            yield scrapy.Request(next_page, callback=self.parse, dont_filter=True, headers=headers)

知道为什么会这样吗?

解决方案可能是为了避免错误,或者可能在引发错误后以某种方式重新启动蜘蛛并恢复抓取过程。

【问题讨论】:

    标签: python web-crawler scrapy-spider


    【解决方案1】:

    BadStatusLine 被引发是因为主机将蜘蛛检测为机器人,并故意断开连接。由于您已经指定了UserAgent,也许您可​​以使用PhantomJS 之类的无头浏览器来模拟浏览器。

    还有一个中间件可以做到这一点:scrapy-webdriver

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多