Python编写的HTTP库,能够发送HTTP和HTTPS请求,并且获取响应。在测试服务器响应方面经常使用。
pip install requests
Requests库中的参数可以分为两个阶段:发起请求时的参数和获取响应后的参数。下面我们来详细看看这两个阶段的参数。
以下是一个示例,展示了如何使用这些参数发起GET请求:
- # 导入requests包
- import requests
- # 自定义请求头
- headers = {"Content-Type": "application/x-www-form-urlencoded"}
- # 添加cookie
- cookies = {'session_id': 'abcdef12345'}
- # 添加请求BADY
- data = "username=admin&password=123456&verifycode=0123"
- # 添加代理
- proxies = {
- 'http':'http://127.0.0.1:8888',
- 'https':'http://127.0.0.1:8888'
- }
- # 发送post请求
- response = requests.get('https://www.baidu.com',data = data,headers = headers,cookies = cookies,proxies = proxies)
注:headers 和 request.headers 是不同的请求头信息,前者是响应的请求头,后者是对于请求所响应
以下是一个示例,展示了如何使用这些参数获取响应的信息:
- # 导入requests包
- >>> import requests
-
- # 发送get请求
- >>> response = requests.get('https://www.baidu.com/s?wd=上海')
-
- # 获取响应的状态码
- >>> status_code = response.status_code
- 200
-
- # 获取响应的响应头
- >>> headers = response.headers
- {'Accept-Ranges': 'bytes', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Content-Length': '227', 'Content-Security-Policy': "frame-ancestors 'self' https://chat.baidu.com http://mirror-chat.baidu.com https://fj-chat.baidu.com https://hba-chat.baidu.com https://hbe-chat.baidu.com https://njjs-chat.baidu.com https://nj-chat.baidu.com https://hna-chat.baidu.com https://hnb-chat.baidu.com http://debug.baidu-int.com;", 'Content-Type': 'text/html', 'Date': 'Tue, 19 Sep 2023 04:48:23 GMT', 'P3p': 'CP=" OTI DSP COR IVA OUR IND COM ", CP=" OTI DSP COR IVA OUR IND COM "', 'Pragma': 'no-cache', 'Server': 'BWS/1.1', 'Set-Cookie': 'BD_NOT_HTTPS=1; path=/; Max-Age=300, BIDUPSID=C6D797F5E9E2D3B9FFE04CE0338CC619; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, PSTM=1695098903; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BAIDUID=C6D797F5E9E2D3B904C8EFA4EBC80FD9:FG=1; max-age=31536000; expires=Wed, 18-Sep-24 04:48:23 GMT; domain=.baidu.com; path=/; version=1; comment=bd', 'Traceid': '1695098903054653569015203487008619068969', 'X-Ua-Compatible': 'IE=Edge,chrome=1'}
-
- # 获取响应对应请求的请求头
- >>> header = response.request.headers
- {'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
-
- # 获取响应的编码格式
- >>> encoding = response.encoding
- ISO-8859-1
-
- # 获取响应的二进制信息(响应体)
- >>> content = response.content
- b'\r\n\r\n\t\r\n\r\n\r\n\t\r\n\r\n'
-
- # 获取响应的文本内容
- >>> text = response.text
-
- # 获取响应的cookie
- >>> cookie = response.cookies
1 for www.baidu.com/>, for .baidu.com/>, 1695099613 for .baidu.com/>, 1 for .baidu.com/>]> -
会话管理
- import requests
-
- session = requests.Session()
- session.get('https://www.baidu.com/login', params={'user': 'username', 'pass': 'password'})
- response = session.get('https://www.baidu.com/dashboard')
文件上传
- import requests
-
- files = {'file': open('文件名+后缀', 'rb')}
- response = requests.post('https://example.com/upload', files=files)
代理设置
- import requests
-
- proxies = {'http': 'http://proxy.example.com', 'https': 'https://proxy.example.com'}
- response = requests.get('https://example.com', proxies=proxies)
使用get获取图片
- import requests
- resp = requests.get('https://pic2.zhimg.com/v2-dad4bf18cb546d9c52ade85d4fba6225_r.jpg')
- # Response对象.content: 返回响应的二进制字节串
- print(resp.content)
- with open('宋茜.jpg', 'wb') as f:
- f.write(resp.content)
请求验证
- import requests
- # verify默认为True,意思是需要做证书校验
- # 如果不希望检查证书,可以将verify设置为False,否则需要指定cert关键字,给出证书的路径
- # 针对HTTPS请求做证书校验
- resp = requests.get('https://www.woniuxy.com/', verify=True, cert='C://tls.pem')
- # 不做证书校验
- resp2 = requests.get('https://www.woniuxy.com/', verify=False)
- print(resp.status_code)
- print(resp.text)