我正在尝试使用scrapy和scrapy-splash获取请求状态代码,下面是spider代码。如何从scrapy-splash获取200以外的状态代码
class Exp10itSpider(scrapy.Spider):
name = "exp10it"
def start_requests(self):
urls = [
'http://192.168.8.240:8000/xxxx'
]
for url in urls:
#yield SplashRequest(url, self.parse, args={'wait': 0.5, 'dont_redirect': True},meta={'handle_httpstatus_all': True})
#yield scrapy.Request(url, self.parse, meta={'handle_httpstatus_all': True})
yield scrapy.Request(url, self.parse, meta={'handle_httpstatus_all': True,'splash': {
'args': {
'html': 1,
'png': 1,
}
}
}
)
def parse(self, response):
input("start .........")
print("status code is:\n")
input(response.status)
我的起始URL http://192.168.8.240:8000/xxxx
是404个状态码的网址,也有在threee种请求方式:
第一个是:
yield SplashRequest(url, self.parse, args={'wait': 0.5, 'dont_redirect': True},meta={'handle_httpstatus_all': True})
第二个是:
yield scrapy.Request(url, self.parse, meta={'handle_httpstatus_all': True})
第三个是:
yield scrapy.Request(url, self.parse, meta={'handle_httpstatus_all': True,'splash': { 'args': { 'html': 1, 'png': 1, } } } )
只有第二个请求的方式yield scrapy.Request(url, self.parse, meta={'handle_httpstatus_all': True})
可以得到正确的状态代码404
,第一和第三都得到状态代码200
,这是说,以后我尝试使用scrapy -splash,我无法获得正确的状态代码404
,你能帮我吗?
我尝试使用下面的代码,http_status_from_error_code = True,但仍然失败。 'yield scrapy.Request(url,self.parse,meta = {'handle_httpstatus_all':True,'splash':{ 'args':{ 'html':1, 'png':1, }, 'magic_response':True, 'http_status_from_error_code':True } } )''' –
为什么不使用'SplashRequest'?这是与Scrapy一起使用Splash的推荐方式。 –
我尝试使用下面的代码SplashRequest,但仍然失败.'yield SplashRequest(url,self.parse,args = {'wait':0.5,'dont_redirect':True},meta = {'handle_httpstatus_all':True}) '我使用不正确? –