当我使用请求库发出请求时,它可以工作:
import requests
r = requests.get("https://www.opentable.de/c/raum-stuttgart-restaurants")
print r
回复是200:
回复[200]
但是当我尝试用Scrapy做同样的事情时,我得到500响应状态:
# -*- coding: utf-8 -*-
from scrapy.spiders import Spider
from scrapy.http import Request
from opentable.items import OpentableItem
class GermanySpider(Spider):
name = "germany_spider"
start_urls = [
"https://www.opentable.de/c/raum-stuttgart-restaurants"
]
def parse(self, response):
pass
日志:
2017-05-30 23:28:33 [scrapy] INFO: Scrapy 1.0.4 started (bot: opentable)
2017-05-30 23:28:33 [scrapy] INFO: Optional features available: ssl, http11
2017-05-30 23:28:33 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'opentable.spiders', 'SPIDER_MODULES': ['op
entable.spiders'], 'BOT_NAME': 'opentable'}
2017-05-30 23:28:36 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2017-05-30 23:28:38 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAg
entMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMidd
leware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2017-05-30 23:28:39 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware
, UrlLengthMiddleware, DepthMiddleware
2017-05-30 23:28:39 [scrapy] INFO: Enabled item pipelines:
2017-05-30 23:28:39 [scrapy] INFO: Spider opened
2017-05-30 23:28:39 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-05-30 23:28:39 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-05-30 23:28:42 [scrapy] DEBUG: Retrying <GET https://www.opentable.de/rest_list.aspx?m=227> (failed 1 times): 500 I
nternal Server Error
2017-05-30 23:28:43 [scrapy] DEBUG: Retrying <GET https://www.opentable.de/rest_list.aspx?m=227> (failed 2 times): 500 I
nternal Server Error
2017-05-30 23:28:44 [scrapy] DEBUG: Gave up retrying <GET https://www.opentable.de/rest_list.aspx?m=227> (failed 3 times
): 500 Internal Server Error
2017-05-30 23:28:44 [scrapy] DEBUG: Crawled (500) <GET https://www.opentable.de/rest_list.aspx?m=227> (referer: None)
2017-05-30 23:28:44 [scrapy] DEBUG: Ignoring response <500 https://www.opentable.de/rest_list.aspx?m=227>: HTTP status c
ode is not handled or not allowed
2017-05-30 23:28:44 [scrapy] INFO: Closing spider (finished)
2017-05-30 23:28:44 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 1344,
'downloader/request_count': 3,
'downloader/request_method_count/GET': 3,
'downloader/response_bytes': 1837,
'downloader/response_count': 3,
'downloader/response_status_count/500': 3,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2017, 5, 30, 18, 28, 44, 745000),
'log_count/DEBUG': 6,
'log_count/INFO': 7,
'response_received_count': 1,
'scheduler/dequeued': 3,
'scheduler/dequeued/memory': 3,
'scheduler/enqueued': 3,
'scheduler/enqueued/memory': 3,
'start_time': datetime.datetime(2017, 5, 30, 18, 28, 39, 76000)}
2017-05-30 23:28:44 [scrapy] INFO: Spider closed (finished)
在scrapinghub有同样的回应。 为什么会这样?请求请求与Scrapy请求之间的区别是什么?
答案 0 :(得分:0)
我遇到了同样的问题,在调试Scrapy请求之后,我发现它默认发送这些标头:
接受: text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8
接受编码:gzip,压缩
对于我来说,是 Accept 标头导致了错误500。将其设置为* / *后,服务器返回了200响应。