安装
pip install requests
请求方式
requests.get(url)
requests.post(url)
requests.put(url)
requests.delete(url)
requests.head(url)
requests.options(url)
Session.request参数
method: 请求方式 如:get,post,put,delete,head,options
url: 请求地址
params: (可选)要在`Request`的查询字符串中发送的字典或字节。
data: (可选)字典,元组列表,字节或类似文件的对象,在`Request`的主体中发送。
json: (可选)json发送到`Request`的正文。
headers: (可选)要使用`Request`发送的HTTP标头字典。
cookies: (可选)使用`Request`发送的Dict或CookieJar对象。
files: (可选)一个字典'name':file-like-objects(或{'name':file-tuple})用于多部分编码上传.file-tuple可以是2 -tuple('filename',fileobj),3-tuple('filename',fileobj,'content_type')或4-tuple('filename',fileobj,'content_type', custom_headers),其中'content-type'是一个定义给定文件内容类型的字符串,custom_headers是一个类似dict的对象,包含为文件添加的附加标题。
auth: (可选)Auth tuple或callable以启用Basic / Digest / Custom HTTP Auth。
timeout: (可选)在放弃之前等待服务器发送数据的时间,浮点数,或者:ref:`(连接超时,读取超时)<超时>`元组。:type timeout: float or tuple
allow_redirects: (可选)是否允许重定向,默认设置为True。:type allow_redirects: bool
proxies: (可选)字典映射协议或协议和主机名到代理的URL。
stream: (可选)是否立即下载响应内容。 默认为“False”。
verify: (可选)布尔值,在这种情况下,它控制我们是否验证服务器的TLS证书或字符串,在这种情况下,它必须是要使用的CA包的路径。 默认为“True”。
cert: (可选)如果字符串,ssl客户端证书文件(.pem)的路径。 如果Tuple,('cert','key')配对。
带参数GET请求
import requests
url = "https://www.baidu.com/s"
data = {
"wd": "python"
}
result = requests.get(url=url, params=data)
print(result.content)
解析json
import requests
url = "https://www.baidu.com/s"
data = {
"wd": "python"
}
result = requests.get(url=url, params=data)
result_json = result.json()
print(result_json)
获取二进制内容
import requests
url = "https://csdnimg.cn/pubfooter/images/csdn-kf.png"
result_bit = requests.get(url=url)
print(result_bit.content)
下载二进制数据
import requests
url = "https://csdnimg.cn/pubfooter/images/csdn-kf.png"
result_bit = requests.get(url=url)
with open("img.png",'wb') as img:
img.write(result_bit.content)
添加headers
import requests
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
}
url = "https://www.baidu.com/s"
data = {
"wd": "python"
}
result = requests.get(url=url, params=data)
print(result.text)
基本post请求
import requests
params = {
'wd': "python"
}
data = {
'account': "python"
}
url = "https://www.baidu.com/s"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
}
result = requests.get(url=url, data=data, params=params, headers=headers)
文件上传
import requests
url = "https://www.baidu.com/s"
files = {'file_name': open("img.png", 'rb')}
result = requests.post(url=url, files=files)
print(result.text)
获取cookie
import requests
url = "https://www.baidu.com/"
result = requests.get(url=url)
cookies = result.cookies
for key, value in cookies.items():
print("{}-{}".format(key, value))
会话维持
import requests
url_login = ""
data = {
'account': "",
'password': ""
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
}
url_get_data = ""
session = requests.Session()
session.post(url=url_login, data=data, headers=headers)
result = session.get(url=url_get_data, headers=headers)
print(result.text)
证书验证
import requests
from requests.packages import urllib3
# 去除不验证证书的警告信息
urllib3.disable_warnings()
url = "https://www.baidu.com/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
}
result = requests.get(url=url, headers=headers, verify=False)
print(result.status_code)
import requests
url = "https://www.baidu.com/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
}
result = requests.get(url=url, headers=headers, cert=('server.cet', 'key'))
print(result.status_code)
代理设置
import requests
url = "https://www.baidu.com/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
}
proxies = {
'http': 'http://127.0.0.1:8888', # 不带账户密码验证
'https': 'https://user:password@127.0.0.1:8888', # 带账户密码验证
}
result = requests.get(url=url, headers=headers, proxies=proxies)
print(result.status_code)
socks代理
先安装requests[socks]模块
pip install requests[socks]
import requests
url = "https://www.baidu.com/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
}
proxies = {
'http': 'socks5://127.0.0.1:8888',
'https': 'socks4://127.0.0.1:8888',
}
result = requests.get(url=url, headers=headers, proxies=proxies)
print(result.status_code)
超时设置
import requests
from requests.exceptions import ConnectTimeout
url = "https://www.google.com/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
}
try:
result = requests.get(url=url, headers=headers, timeout=1)
except ConnectTimeout:
print("超时")
认证设置
import requests
from requests.auth import HTTPBasicAuth
url = "https://test.daqinjia.cn/wx/api/qlive/get_livelist/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36",
}
result = requests.get(url=url, headers=headers, auth=HTTPBasicAuth("account", "password"))
print(result.status_code)
import requests
url = "https://test.daqinjia.cn/wx/api/qlive/get_livelist/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36",
}
auth = ("account", "password")
result = requests.get(url=url, headers=headers, auth=auth)
print(result.status_code)
异常处理
处理requests的异常类都在requests.exceptions里
from requests.exceptions import *
可以根据需要调取