整理requests库的使用,主要记录Seesion 对象的用法
import requests
session = requests.Session()
response = session.get(url)
s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})
# x-test , x-test2都会被发送
s.get('https://httpbin.org/headers', headers={'x-test2': 'true'})
s = requests.Session()
r = s.get('https://httpbin.org/cookies', cookies={'from-my': 'browser'})
print(r.text)
r = s.get('https://httpbin.org/cookies')
print(r.text)
s = requests.Session()
r = s.get('https://httpbin.org/cookies', cookies={'from-my': None})#设置为None
主要依赖于
from requests import Request, Session
s = Session()
req = Request('POST', url, data=data, headers=headers)#初始化requests
prepped = req.prepare()#调用prepare,获得参数设置
# do something with prepped.body
prepped.body = 'No, I want exactly this as the body.'#修改Body
# do something with prepped.headers
del prepped.headers['Content-Type']#修改请求头
resp = s.send(prepped,#发送请求
stream=stream,
verify=verify,
proxies=proxies,
cert=cert,
timeout=timeout
)
print(resp.status_code)
from requests import Request, Session
s = Session()
req = Request('POST', url, data=data, headers=headers)
prepped = req.prepare()
# do something with prepped.body
prepped.body = 'No, I want exactly this as the body.'
# do something with prepped.headers
del prepped.headers['Content-Type']
resp = s.send(prepped,
stream=stream,
verify=verify,
proxies=proxies,
cert=cert,
timeout=timeout
)
print(resp.status_code)
requests.get('https://github.com', verify='/path/to/certfile')
#or
s = requests.Session()
s.verify = '/path/to/certfile'
tarball_url = 'https://github.com/requests/requests/tarball/master'
r = requests.get(tarball_url, stream=True)
import requests
proxies = {
'http': 'http://10.10.1.10:3128',
'https': 'http://10.10.1.10:1080',
}
requests.get('http://example.org', proxies=proxies)
proxies = {
'http': 'socks5://user:pass@host:port',
'https': 'socks5://user:pass@host:port'
}