post数据

import requests

url = 'http://httpbin.org/post'
d = {
    "type": "1",
    "name": "",
    "area": "",
    "city": "不限",
    "city1": "----",
    "city2": "----",
    "xmxz": "0",
    "state": "0",
    "money": "",
    "money1": "",
    "number": "0",
    "fid1": "",
    "fid2": "",
    "fid3": "",
    "order": "0",
    "page": "1",
    "include": "0"
}
r = requests.post(url, data=d)
print(r.text)
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "area": "", 
    "city": "\u4e0d\u9650", 
    "city1": "----", 
    "city2": "----", 
    "fid1": "", 
    "fid2": "", 
    "fid3": "", 
    "include": "0", 
    "money": "", 
    "money1": "", 
    "name": "", 
    "number": "0", 
    "order": "0", 
    "page": "1", 
    "state": "0", 
    "type": "1", 
    "xmxz": "0"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "146", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.31.0", 
    "X-Amzn-Trace-Id": "Root=1-64a7c14b-08a0b09e61d2ca73754b4214"
  }, 
  "json": null, 
  "origin": "36.155.28.104", 
  "url": "http://httpbin.org/post"
}

可以看出,post的数据直接使用表单数据字典格式的数据即可。直接用火狐的请求--表单数据--全部复制,就是现成的字典。

如果内容类型是json,要在headers里指明,'Content-Type': "application/json",并将数据进行json编码,data = json.dumps(d)。

或者requests中使用json参数,json=d。

参考文章:https://blog.csdn.net/dta0502/article/details/90214562

posted @ 2023-07-07 15:52  sbsdnyn  阅读(32)  评论(0)    收藏  举报