本文已参与「新人创作礼」活动,一起开启掘金创作之路。
requests 模块
安装requests 模块
打开cmd 窗口
pip install -i pypi.tuna.tsinghua.edu.cn/simple requests
*HTTP方法
| GET | 获取资源 |
|---|---|
| POST | 传输实体主体 |
| PUT | 传输文件 |
| HEDA | 获得响应报文首部 |
| DELETE | 删除文件 |
| OPTIONS | 查询支持的方法 |
| TRACK | 追踪路径 |
| CONNECT | 要求用隧道协议连接代理 |
| LINK | 建立呵资源之间的连接 |
| UNLINK | 断开连接 |
*requests 模块中的http方法
res = reuqests.get()
…
*参数
| GET参数 | params |
|---|---|
| HTTP头部 | headers |
| POST参数 | data |
| 文件 | files |
| Cookies | cookies |
| 重定向处理 | allow_redirects = False/True |
| 超时 | timeout |
| 证书验证 | verify = False/True |
| 工作流(延迟下载) | stream = False/True |
| 事件挂钩 | hooks = dict(response=) |
| 身份验证 | auth = |
| 代理 | proxies = |
*对象方法
| URL | .url | |
|---|---|---|
| text | .text | |
| 编码 | .excoding | .encoding= |
| 响应内容 | .content | |
| Json 解码器 | .json | |
| 原始套接字响应 | .raw | .raw.read() |
| 历史响应代码 | .history | |
| 抛出异常 | .raise_for_status() | |
| 查看服务器响应头 | .headers | |
| 查看客户端请求头 | .request.headers | |
| 查看Cookie | .cookies | |
| 身份验证 | .auth= | |
| 更新 | .update | |
| 解析连接字头 | .links[] |
*模块入门
导入模块
import requests
发送简洁请求
发送get 请求
res = requests.get("http://192.168.1.200/php/get.php")
相关方法
获取响应正文
res.txt
获取响应状态码
res.status_code
获取响应编码
res.encoding
以二进制方式获取相应正文
res.content
获取响应头
res.headers
获取提交的URL(包括GET 参数)
res.url
获取发送到服务器的头信息
res.request.headers
例如:
----------get.php
import requests
res = requests.get("http://192.168.1.200/php/get.php")
res.text
'array(0) {\n}\n'
res.status_code
200
res.encoding
'ISO-8859-1'
res.content
b'array(0) {\n}\n'
res.headers
{'Date': 'Thu, 14 May 2020 00:57:37 GMT', 'Server': 'Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45', 'X-Powered-By': 'PHP/5.4.45', 'Content-Length': '13', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html'}
res.request.headers
{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive'}
res.url
'http://192.168.1.200/php/get.php'
*相关操作
定制头部
import requests
url = "http://192.168.1.200/php/get.php"
header = {"User-Agent":"GGG"}
res = requests.get(url=url,headers=header)
print(res.request.headers)
{'User-Agent': 'GGG', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive'}
超时
-----timeout.php
import requests
url = "http://192.168.1.200/php/timeout.php"
try:
res = requests.get(url=url,timeout=2)
print(res.text)
except Exception as e:
print("TimeOut")
TimeOut
GET 传参
import requests
url = "http://192.168.1.200/php/get.php"
getPara = {"name":"GGG","pwd":"123456"}
res = requests.get(usr=url,params=getPara)
print(res.text)
print(res.url)
array(2) {
["name"]=>
string(3) "GGG"
["pwd"]=>
string(6) "123456"
}
http://192.168.1.200/php/get.php?name=GGG&pwd=123456
POST 传参
----------post.php
import requests
url = "http://192.168.1.200/php/post.php"
postData = {"name":"GGG","pwd":"123456"}
res = requests.post(url=url,data=postData)
print(res.text)
array(2) {
["name"]=>
string(3) "GGG"
["pwd"]=>
string(6) "123456"
}
上传文件
----------upfile.php
文件上传测试
action=""
method="post"
enctype="multipart/form-data"
"; if(isset($_POST['userSubmit'])){ var_dump($_FILES); $tmp_path=$_FILES['userUpFile']['tmp_name']; $path=__DIR__."\\".$_FILES['userUpFile']['name'];//__DIR__获取当前php脚本所在目录 //echo $path; if(move_uploaded_file($tmp_path,$path)){ //move_uploaded_file(参数1,参数2);将上传上来的缓存文件的目录(参数1)保存到参数2目录下 echo "upfile success!"; echo "
".$_FILES['userUpFile']['name']; }else{ echo "upfile failed"; } } ?>
import requests
url = "http://192.168.1.200/PHP/array/up/upfile.php"
upFile = {"userUpFile":open("postData.py","rb")}
postData = {"userSubmit":"上传"}
res = requests.post(url=url,files=upFile,data=postData)
print(res.text)
array(1) { ["userUpFile"]=> array(5) { ["name"]=> string(11) "postData.py" ["type"]=> string(0) "" ["tmp_name"]=> string(31) "C:\phpStudy\tmp\tmp\phpEE47.tmp" ["error"]=> int(0) ["size"]=> int(171) } } upfile success!
postData.py >>> ----------------------
重定向
------redirect.php
import requests
url = "http://192.168.1.200/php/redirect.php"
res = requests.get(url=url)
print(res.text)
print(res.history)
print('\n')
res = requests.get(url=url,allow_redirects=False)
print(res.headers)
print(res.text)
array(0) {
}
[<Response [302]>]
{'Date': 'Thu, 14 May 2020 09:02:34 GMT', 'Server': 'Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45', 'X-Powered-By': 'PHP/5.4.45', 'location': './get.php', 'Content-Length': '21', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html'}
This is redirect.php!
关于cookies
-----cookie.php
import requests
url = "http://192.168.1.200/php/cookie.php"
Coo = {"name":"GGG"}
res = requests.get(url=url,cookies=Coo)
print(res.text)
array(1) {
["name"]=>
string(3) "GGG"
}
参考文献: