import requests
s = requests.Session()
#传递url参数
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
#传递表单数据
data = {'key1':'value1','key2':'value2'}
r2 = requests.post('http://httpbin.org/post',data = data)
print(r2.text)
# 定制请求头部
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)
跨站请求时方法中级别的参数不会保存,会话中的参数会保存。
#解码json文件
r3 =requests.get('https://github.com/timeline.json')#该链接返回json数据,方便测试。
print(r3.text)
print(r3.json())#解码json文件
网友评论