Ruquests

2023-07-30 17:28:06 浏览数 (1)

中文文档:https://requests.readthedocs.io/projects/cn/zh_CN/latest/

快捷请求:

代码语言:javascript复制
url  ='https://leafbackaut.cn'

#  get请求
r = requests.get(url)
#  post请求
r = requests.post(url)
#  put请求
r = requests.put(url)
#  delete请求
r = requests.delete(url)
#  head请求
r = requests.head(url)
#  options请求
r = requests.options(url)

请求参数

get传参:

代码语言:javascript复制
url  ='https://leafbackaut.cn'

#  普通传参
#  https://leafbackaut.cn?key2=value2&key1=value1
args = {'key1': 'value1', 'key2': 'value2'}
r = requests.get(url, params=args)
#  列表传参
#  https://leafbackaut.cn?key1=value1&key2=value2&key2=value3
args = {'key1': 'value1', 'key2': ['value2', 'value3']}
r = requests.get(url, params=args)

post传参:

代码语言:javascript复制
url  ='https://leafbackaut.cn'

#  普通传参
args = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=args)
#  一对多传参(多个元素使用一个key)
args = (('key1', 'value1'), ('key1', 'value2'))
r = requests.post(url, data=args)
#  传递json参数
args = {'some': 'data'}
r = requests.post(url, json=args)
r = requests.post(url, data=json.dumps(args))

响应内容:

代码语言:javascript复制
url  ='https://leafbackaut.cn'
r = requests.get(url)

#  响应的文本(Requests 会自动解码来自服务器的内容。大多数 unicode 字符集都能被无缝地解码。)
r.text
#  改变响应文本的编码
r.encoding = 'UTF-8'

#  二进制响应内容
r.content
#  tip:使用二进制响应内容创建一张图片
from PIL import Image
from io import BytesIO
i = Image.open(BytesIO(r.content))

#  json响应内容
r.json()

#  原始响应内容(确保在原始请求中设置了stream=True)
r.raw

自定义请求头:

代码语言:javascript复制
url = 'https://leafbackaut.cn'
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)

响应状态码:

代码语言:javascript复制
r = requests.get('https://leafbackaut.cn')
r.status_code
#  判断状态码是否ok
r.status_code == requests.codes.ok

响应头与请求头:

代码语言:javascript复制
url  ='https://leafbackaut.cn'
r = requests.get(url)
#  获取响应头
r.headers
#  获取请求头
r.request.headers

Cookie:

代码语言:javascript复制
url = 'https://leafbackaut.cn'
#  获取cookie
r = requests.get(url)
r.cookies

#  添加cookie
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)

#  操作cookie对象
#  适合跨域名或跨路径使用
jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain=url, path='/path1')
jar.set('gross_cookie', 'blech', domain=url, path='/path2')
#  使用第一个cookie
r1 = requests.get(url '/path2', cookies=jar)
#  使用第二个cookie
r2 = requests.get(url '/path2', cookies=jar)

请求历史与重定向:

代码语言:javascript复制
url  ='https://leafbackaut.cn'
r = requests.get(url)
r.history
#  禁用重定向
r = requests.get(url, allow_redirects=False)
#  如果使用head请求,则需要设置参数allow_redirects=True
r = requests.head(url, allow_redirects=True)
r.history

设置超时:

代码语言:javascript复制
url  ='https://leafbackaut.cn'
#  同时设置connect和read的timeout
r = requests.get(url, timeout=10)
#  分别设置connect和read的timeout
r = requests.get(url, timeout=(10, 20))
#  永远等待
r = request.get(url, timeout=None)

会话对象: 会话对象在同一个Session实例发出的所有请求之间保持cookie。

代码语言:javascript复制
url = 'https://leafbackaut.cn'
s = requests.Session()
#  请求第一个网站,设置cookie
s.get(url)
#  不同的请求得到相同的cookie
r1 = s.get(url '/path1')
r2 = s.get(url '/path2')
r1.cookies
r2.cookies

#  会话对象设置的参数会同步到请求方法
s = requests.Session()
#  通过会话对象设置请求头
s.headers.update({'x-test1': 'true'})
#  通过请求方法设置请求头
args = {'x-test2': 'true'}
#  此时请求头中包含x-test1和x-test2
s.get(url, headers=args)
s.headers

#  请求方法设置的参数不会影响到会话对象和其他请求方法
s = requests.Session()
#  不同的请求方法互不影响
r1 = s.get(url, cookies={'from-my': 'browser'})
r2 = s.get(url)
#  r1有cookie,r2无cookie
r1.cookies
r2.cookies

SSL证书验证:

代码语言:javascript复制
url = 'https://leafbackaut.cn'
verify_path = '/certfile'
#  在请求中设置证书
r = requests.get(url, verify=verify_path)
#  在会话中设置证书
s = requests.Session()
s.verify = verify
#  不设置证书(默认开启使用内置的证书)
requests.get(url, verify=False)

客户端证书:

代码语言:javascript复制
url = 'https://leafbackaut.cn'
cert_path=('/path/client.cert', '/path/client.key')
#  在请求方法中设置证书
r = requests.get(url, cert=cert_path)
#  在会话中设置证书
s = requests.Session()
s.cert = '/path/client.cert'

设置代理:

代码语言:javascript复制
url = 'https://leafbackaut.cn'
proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}
r = reuqests.get(url, proxies=proxies)

0 人点赞