Requests方法 -- post

>>> import requests  导入requests库

>>> help(requests)  #查看requests方法
Help on package requests:

NAME
requests

DESCRIPTION
Requests HTTP Library
~~~~~~~~~~~~~~~~~~~~~

Requests is an HTTP library, written in Python, for human beings. Basic GET
usage:

>>> import requests
>>> r = requests.get('https://www.python.org')
>>> r.status_code
200
>>> 'Python is a programming language' in r.content
True

... or POST:

>>> payload = dict(key1='value1', key2='value2')
>>> r = requests.post('https://httpbin.org/post', data=payload)
>>> print(r.text)
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}

The other HTTP methods are supported - see `requests.api`. Full documentation
is at <http://python-requests.org>.

:copyright: (c) 2017 by Kenneth Reitz.
:license: Apache 2.0, see LICENSE for more details.

做post实例之字典序列化操作:

>>> import requests
>>> import json
>>> payload = {"yy":"1123","gg":"4145"}
>>> data_json = json.dumps(payload)  # 字典序列化转化用dumps
>>> r = requests.post("https://httpbin.org/post",json=data_json)
>>> print(r.text)
{
"args": {},
"data": ""{\"yy\": \"1123\", \"gg\": \"4145\"}"",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "38",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": "{"yy": "1123", "gg": "4145"}",
"origin": "223.104.1.157, 223.104.1.157",
"url": "https://httpbin.org/post"
}

如下为字典:  ---- >也可以写成  >>> payload =dict(yy="1123",gg="4145") 的格式

>>> payload = {"yy":"1123","gg":"4145"}
>>> r = requests.post("https://httpbin.org/post",data=payload)
>>> print(r.text)
{
"args": {},
"data": "",
"files": {},
"form": {
"gg": "4145",
"yy": "1123"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "15",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": null,
"origin": "223.104.1.157, 223.104.1.157",
"url": "https://httpbin.org/post"
}

如查看某个值: 

>>> print(r.json()["form"]["gg"])
4145

原文地址:https://www.cnblogs.com/Teachertao/p/11141230.html