python - HTTP 状态代码未处理或不允许

标签 python scrapy

我需要使用 Scrapy 抓取带有 cookie 的站点,但返回错误

代码在这里

class XueqiuSpider(scrapy.Spider):
    name = "xueqiu"
    start_urls = ["https://xueqiu.com/stock/f10/finmainindex.json?symbol=SZ000001&page=1&size=1"]
    delimiter = ','
    quotechar = '"'
    headers = ["symbol","date","open","high","low","close","volume"]

    def start_requests(self):
        for i,url in enumerate(self.start_urls):
            print(url)
            yield Request(url,cookies={'aliyungf_tc':'AQAAANiAQ3xQ/QAAZ0J2fRFnxcJufEzG'},callback=self.parse_item)

    def parse_item(self, response):
        print response

错误显示

********Current UserAgent:Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11************
2017-03-02 18:56:02 [scrapy.downloadermiddlewares.cookies] DEBUG: Sending cookies to: <GET https://xueqiu.com/stock/f10/finmainindex.json?symbol=SZ000001&page=1&size=1>
Cookie: aliyungf_tc=AQAAANiAQ3xQ/QAAZ0J2fRFnxcJufEzG; aliyungf_tc=AQAAAM/c+1g5vAMAZ0J2fbusPyBy7jb1

2017-03-02 18:56:02 [scrapy.core.engine] DEBUG: Crawled (400) <GET https://xueqiu.com/stock/f10/finmainindex.json?symbol=SZ000001&page=1&size=1> (referer: None)
2017-03-02 18:56:02 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <400 https://xueqiu.com/stock/f10/finmainindex.json?symbol=SZ000001&page=1&size=1>: HTTP status code is not handled or not allowed

最佳答案

正如您在 doc 中所读到的那样,根据HTTP标准,成功的响应是那些状态码在200-300范围内的响应。

如果您仍想处理超出该范围的响应代码,您可以使用 handle_httpstatus_list 爬虫属性或 HTTPERROR_ALLOWED_CODES 设置指定爬虫能够处理的响应代码。

所以你应该在你的代码中添加它

class XueqiuSpider(scrapy.Spider):
    handle_httpstatus_list = [400]

关于python - HTTP 状态代码未处理或不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553821/

相关文章:

python - Tkinter:更改窗口顶部栏的颜色

PythonWebkit 默认目录列表

facebook - 使用 scrapy 从 facebook 中抓取数据

python - 使用Pipeline.py删除Value而不是Field

python - Scrapy 分页在多个列表上失败

Debian Lenny 上的 Python 2.6。可执行文件应该去哪里?

python - conda 可以从 env environment.yml 自动激活 env 吗?

python - 编程新手。尝试使用 python 3 将相同的文本添加到列中所有数据条目的末尾

python - 来自官方 Github 的 ScrapyJS 示例未运行

python - 无法使用 scrapy 蜘蛛抓取特定网站的元素