import requests
s = requests.Session()
#傳遞url參數(shù)
dic = {'key1':'value1','key2':'value2','key3':['value3','value4']}
r1 = requests.get('http://httpbin.org/get',params = dic)
print(r1.url)#http://httpbin.org/get?key3=value3&key3=value4&key2=value2&key1=value1
#傳遞表單數(shù)據(jù)
data = {'key1':'value1','key2':'value2'}
r2 = requests.post('http://httpbin.org/post',data = data)
print(r2.text)
# 定制請(qǐng)求頭部
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
r3 = requests.get(url, headers=headers)
# 傳文件
url = 'http://httpbin.org/post'
files = {'file': open('report.txt', 'rb')}
r = requests.post(url, files=files)
跨站請(qǐng)求時(shí)方法中級(jí)別的參數(shù)不會(huì)保存,會(huì)話中的參數(shù)會(huì)保存。
#解碼json文件
r3 =requests.get('https://github.com/timeline.json')#該鏈接返回json數(shù)據(jù),方便測(cè)試。
print(r3.text)
print(r3.json())#解碼json文件