Scrapy 404错误:未处理或不允许HTTP状态代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scrapy 404错误:未处理或不允许HTTP状态代码相关的知识,希望对你有一定的参考价值。

我正在尝试使用scrapy刮取网站coursetalk,我正在尝试使用蜘蛛模板并获得404错误:

2017-12-29 23:34:30 [scrapy] DEBUG: Ignoring response <404 https://www.coursetalk.com/subjects/data-science/courses/>: HTTP status code is not handled or not allowed

这是我正在使用的代码:

import scrapy

class ListaDeCursosSpider(scrapy.Spider):
    name = "lista_de_cursos"
    start_urls = ['https://www.coursetalk.com/subjects/data-science/courses/'] 


    def parse(self, response):
        print response.body

来自scrapy的竞争日志:

2017-12-29 23:34:26 [scrapy] INFO: Scrapy 1.0.3 started (bot: coursetalk)
2017-12-29 23:34:26 [scrapy] INFO: Optional features available: ssl, http11, boto
2017-12-29 23:34:26 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'coursetalk.spiders', 'SPIDER_MODULES': ['coursetalk.spiders'], 'BOT_NAME': 'coursetalk'}
2017-12-29 23:34:27 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2017-12-29 23:34:27 [boto] DEBUG: Retrieving credentials from metadata server.
2017-12-29 23:34:28 [boto] ERROR: Caught exception reading instance data
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/boto/utils.py", line 210, in retry_url
    r = opener.open(req, timeout=timeout)
  File "/usr/lib/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 422, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1184, in do_open
    raise URLError(err)
URLError: <urlopen error timed out>
2017-12-29 23:34:28 [boto] ERROR: Unable to read instance data, giving up
2017-12-29 23:34:28 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2017-12-29 23:34:28 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2017-12-29 23:34:28 [scrapy] INFO: Enabled item pipelines: 
2017-12-29 23:34:28 [scrapy] INFO: Spider opened
2017-12-29 23:34:28 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-12-29 23:34:28 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-12-29 23:34:30 [scrapy] DEBUG: Crawled (404) <GET https://www.coursetalk.com/subjects/data-science/courses/> (referer: None)
2017-12-29 23:34:30 [scrapy] DEBUG: Ignoring response <404 https://www.coursetalk.com/subjects/data-science/courses/>: HTTP status code is not handled or not allowed
2017-12-29 23:34:30 [scrapy] INFO: Closing spider (finished)
答案

看起来这个网站是如此奇怪,响应状态代码是404但仍然可以正常获取正文。

在Scrapy中,HttpErrorMiddleware默认启用,这将过滤掉不成功的Http响应,以便蜘蛛不必处理它们。在这种情况下,scrapy提供HTTPERROR_ALLOWED_CODES设置,即使返回错误代码也允许处理响应。

HTTPERROR_ALLOWED_CODES =[404]项目中添加setting.py可以绕过这个问题

import scrapy
import logging

class ListaDeCursosSpider(scrapy.Spider):
    name = "lista_de_cursos"
    allowed_domains = ['www.coursetalk.com']
    start_urls = ['https://www.coursetalk.com/subjects/data-science/courses/'] 

 def parse(self, response):
        logging.info("response.status:%s"%response.status)
        logourl = response.selector.css('div.main-nav__logo img').xpath('@src').extract()
        logging.info('response.logourl:%s'%logourl)
另一答案

我已经用scrapy面对这个问题并解决了它。

USER_AGENT改变了setting.py

USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/34.0.1847.131 Safari/537.36"

以上是关于Scrapy 404错误:未处理或不允许HTTP状态代码的主要内容,如果未能解决你的问题,请参考以下文章

404未找到:原始服务器未找到目标资源的当前表示,或不愿意透露存在的资源

WCF HTTP 错误 404.17 - 未找到 请求的内容似乎是脚本,不会由静态文件处理程序提供

"HTTP 404 未找到"的错误如何对付?_

IIS Express错误http 404.17 - 未找到运行WCF服务

HTTP 错误 404.3 - 未找到 - 由于扩展配置,无法提供您请求的页面

Spring Boot POST 请求返回 http 状态 405“方法不允许”而不是 HTTP 状态 404