Python3 requests库的高级用法

整理requests库的使用,主要记录Seesion 对象的用法

1.创建session

import requests
session = requests.Session()
response = session.get(url)

2.修改session的header

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'})

3.session发送cookies

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)

4.删除session中的cookies

s = requests.Session()

r = s.get('https://httpbin.org/cookies', cookies={'from-my': None})#设置为None

5.自定义request

主要依赖于

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)

使用proxies

http代理

import requests

proxies = {
  'http': 'http://10.10.1.10:3128',
  'https': 'http://10.10.1.10:1080',
}

requests.get('http://example.org', proxies=proxies)

socks代理

proxies = {
    'http': 'socks5://user:pass@host:port',
    'https': 'socks5://user:pass@host:port'
}

你可能感兴趣的:(python)