[英]200 response status with requests library but 500 with Scrapy
When I use make the request with the requests library, it works: 当我使用请求库发出请求时,它可以工作:
import requests
r = requests.get("https://www.opentable.de/c/raum-stuttgart-restaurants")
print r
The response is 200: 回复是200:
Response [200]
回应[200]
But when I try to do the same thing with Scrapy I get 500 response status: 但是当我尝试用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
Log: 日志:
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)
At scrapinghub there is the same response. 在scrapinghub有相同的反应。 Why does it happen?
为什么会这样? What's the difference between a request with requests and a request with Scrapy?
请求请求与Scrapy请求之间有什么区别?
I've got the same problem and, after debugging Scrapy requests, I've found it send these headers by default: 我有同样的问题,在调试Scrapy请求后,我发现它默认发送这些头:
Accept : text/html,application/xhtml+xml,application/xml;q=0.9, / ;q=0.8
接受 :text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8
Accept-Encoding : gzip,deflate
Accept-Encoding :gzip,deflate
For me it was the Accept header that was causing the error 500. After setting it to */* the server returned a 200 response. 对我来说,是导致错误500的Accept标头。在将其设置为* / *之后,服务器返回了200响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.