request是一个简答优雅的python HTTP库,相较于python标准库中的urllib和urllib2的库,requests更加的便于理解和使用.
1. 安装 requests
2. requests包的使用
get
1 2 3 4 5
| >>> payload = {'key1': 'value1', 'key2': 'value2'} >>> r = requests.get('https://httpbin.org/get', params=payload)
>>> print(r.url) https://httpbin.org/get?key2=value2&key1=value1
|
post
1 2 3 4 5 6 7 8 9 10
| import requests import json
headers = {'content-type': 'application/json'} url = 'http://192.168.3.45:8080/api/v2/event/log'
data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}} params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}
requests.post(url, params=params, data=json.dumps(data), headers=headers, timeout=2)
|
parse
1 2
| import requests requests.get(url).json()
|
3. 参考资料: