requests
安装
pip3 install requests
官方中文文档:http://cn.python-requests.org/zh_CN/latest/user/quickstart.html#id4
get
# 1、无参数实例
import requests
ret = requests.get('https://github.com/timeline.json')
print(ret.url)
print(ret.text)
# 2、有参数实例
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.get("http://httpbin.org/get", params=payload)
print(ret.url)
print(ret.text)
POST
# 1、基本POST实例
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.post("http://httpbin.org/post", data=payload)
print(ret.text)
# 2、发送请求头和数据实例
import requests
import json
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
ret = requests.post(url,
data=json.dumps(payload),
headers=headers)
print(ret.text)
print(ret.cookies)
# 上面的等同于
ret = requests.post(url, json=payload)
网友评论