鍍金池/ 問答/人工智能  數(shù)據(jù)分析&挖掘  Python/ scrapy 怎么post 請(qǐng)求payload形式的參數(shù)

scrapy 怎么post 請(qǐng)求payload形式的參數(shù)

有些網(wǎng)站post參數(shù)是payload形式的,我用requests.post(url,json=payload)就可以請(qǐng)求成功,有些是formdata形式的,用requests.post(url,data=formdata)。
但是我用scrapy的時(shí)候用formrequest是不能發(fā)送payload形式的參數(shù)并且請(qǐng)求成功的,那么scrapy怎么post請(qǐng)求payload形式的參數(shù)

回答
編輯回答
巫婆

可以看看這篇博客 http://blog.51cto.com/1292522...

2017年4月7日 14:42
編輯回答
半心人

類似這樣使用!

req = scrapy.FormRequest('http://httpbin.org/post', formdata=data)

例子:

In [29]: req = scrapy.FormRequest('http://httpbin.org/post', formdata=data)

In [30]: fetch(req)

In [31]: response
Out[31]: <200 http://httpbin.org/post>

In [32]: print(response.body_as_unicode())
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "name": "python"
  },
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Encoding": "gzip,deflate",
    "Accept-Language": "en",
    "Connection": "close",
    "Content-Length": "11",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "Scrapy/1.4.0 (+http://scrapy.org)"
  },
  "json": null,
  "origin": "112.96.195.12",
  "url": "http://httpbin.org/post"
}

In [33]: r = requests.post('http://httpbin.org/post', data=data)

In [34]: print(r.text)
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "name": "python"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "11",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.18.4"
  },
  "json": null,
  "origin": "112.96.195.12",
  "url": "http://httpbin.org/post"
}
2017年5月11日 22:08
編輯回答
慢半拍

請(qǐng)問樓主解決了嗎,,,scrapy怎么post請(qǐng)求payload形式的參數(shù)

2017年10月19日 17:06