- A+
1.GET请求
1 2 3 4 5 6 7 8 |
import requests #参数 data={ 'admin':'a' } respose=requests.get("https://baidu.com",params=data) print(respose.text) print(respose.json()) |
2.POST请求
1 2 3 4 5 6 7 8 9 10 11 12 13 |
data={'name':'admin', 'passwd':'123465' } headers={} respose=requests.post('http://httpbin.org/post',data=data,headers=headers) print(respose.json()) # 响应 print(respose.status_code) print(respose.headers) print(respose.cookies) print(respose.url) print(respose.history) |
3.添加header
1 2 3 4 5 |
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'} respose=requests.get("https://httpbin.org/get",headers=headers) print(respose.content.decode('utf-8')) |
4.获取cookie
1 2 3 4 |
respose=requests.get("https://baidu.com") print(respose.cookies) for key ,value in respose.cookies.items(): print(key+'='+value) |
5.代理设置
1 2 3 4 5 6 |
proxies={ 'https':'https://127.0.0.1:9999', 'http':'http://127.0.0.1:9999' } respose=requests.get("http://httpbin.org/get",proxies=proxies) print(respose.text) |
6.认证设置
1 2 3 4 5 6 7 |
from requests.auth import HTTPBasicAuth respose=requests.get('http://x.x.x.x',auth=HTTPBasicAuth('root','****')) print(respose.content.decode('utf-8')) print(respose.url) with open('dyan.html','wb') as w: w.write(respose.content) w.close() |
7.文件上传
1 2 3 |
files={'file':open('1.jpg','rb')} respose=requests.post('http://httpbin.org/post',files=files) print(respose.text) |
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫