将标头和有效负载导入Scrapy

时间:2016-05-25 15:21:55

标签: python scrapy

我一直在使用firebug,我已经有以下词典来查询api。

payload = "{\"prefixText\":\"2261\",\"count\":\"10 \"}"

headers = {
'origin': "site.com",
'x-requested-with': "XMLHttpRequest",
'user-agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36",
'content-type': "application/json; charset=UTF-8",
'accept': "*/*",
'referer': "***",
'accept-encoding': "gzip, deflate",
'accept-language': "en-US,en;q=0.8",
'cookie': "ASP.NET_SessionId=tnrqoff2y; 
'cache-control': "no-cache"
}

使用python请求,使用它就像:

一样简单
response = requests.request("POST", url, data=payload, headers=headers)

我如何在Scrapy中使用它们?我知道它与中间件有关,但我一直在阅读http://doc.scrapy.org/en/latest/topics/spider-middleware.html并且我不清楚如何做到这一点。

1 个答案:

答案 0 :(得分:1)

Scrapy对于“发出请求”并不是很有用,那就是requests模块。 Scrapy是一个爬行框架,用于创建网站蜘蛛,对于那些蜘蛛,请求规则是必​​要的。

无论如何,如果您创建了一个蜘蛛,并且需要发送POST请求,您可以这样做:

...
yield Request(url, method="post", headers=dict(), body=body, callback=self.parse_method)
...

我建议先关注this tutorial