一、安装模块
pip install requests
二、引用
import requests
三、get方法
#GET访问页面
r = requests.get(url)
print(r.text)
#GET传参数
r = requests.get('https://www.douban.com/search',params={'q':'python','cat':'1001'})
print(r.url)
#https://www.douban.com/search?q=python&cat=1001
#传递Header头,需要headers的参数
r = requests.get('https://www.douban.com/', headers={'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit'})
四、post方法
#post方法,传递需要data参数
r = requests.post('https://accounts.douban.com/login', data={'form_email': 'abc@example.com', 'form_password': '123456'})
#传递json参数
params = {'key': 'value'}
r = requests.post(url, json=params)
#上传文件类编码格式
upload_files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=upload_files)
五、其他参数
1.传递cookies参数
cs = {'token': '12345', 'status': 'working'}
r = requests.get(url, cookies=cs)