不处理或不允许HTTP状态代码

时间:2017-03-02 11:10:23

标签: 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

1 个答案:

答案 0 :(得分:1)

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

如果您仍想处理该范围之外的响应代码,可以使用handle_httpstatus_list spider属性或HTTPERROR_ALLOWED_CODES设置指定蜘蛛能够处理的响应代码。

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

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