【问题标题】:200 response status with requests library but 500 with Scrapy请求库的响应状态为 200,但 Scrapy 的响应状态为 500
【发布时间】:2017-10-31 08:35:50
【问题描述】:

当我使用 requests 库发出请求时,它可以工作:

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 的请求有什么区别?

【问题讨论】:

  • 您正在访问两个不同的 URL - 或者更确切地说,Scrapy 正在尝试从初始化页面加载链接并且这些链接抛出错误 500。
  • 你使用什么标题?该网站可能会禁止带有默认 HTTP 标头的请求。
  • 看起来像一个用户代理字符串的东西。尝试将您的用户代理更改为真正的桌面浏览器或“Mozilla”,例如您的 settings.py 中的USER_AGENT="Mozilla"

标签: python web-scraping scrapy python-requests


【解决方案1】:

我也遇到了同样的问题,在调试 Scrapy 请求后,我发现它默认发送这些标头:

接受: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

接受编码:gzip,deflate

对我来说,是 Accept 标头导致错误 500。将其设置为 */* 后,服务器返回 200 响应。

【讨论】:

    猜你喜欢
    • 2023-02-20
    • 1970-01-01
    • 2016-11-29
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    相关资源
    最近更新 更多