requests快速上手
- 安装
pip3 install requests
- 发送一个http请求
import requests
r = requests.get('https://www.python.org')
r = requests.post('https://httpbin.org/post', data={'key': 'value'})
r = requests.put('https://httpbin.org/put', data={'key':'value'})
r = requests.delete('https://httpbin.org/delete')
r = requests.head('https://httpbin.org/get')
r = requests.options('https://httpbin.org/get')
- 传递url参数
import requests
payload = {'key': 'value'}
r = requests.get('https://httpbin.org/get', params=payload)
print(r.url)
- 响应内容
import requests
r = requests.get('https://python.org')
print(r.text)
print(r.encoding)
- 二进制相应内容
import requests
r = requests.get('https://www.python.org')
print(r.content)
- json响应内容
import requests
r = requests.get('https://www.python.org')
print(r.json())
- 原始响应内容
import requests
r = requests.get('https://api.github.com/events', stream=True)
print(r.raw)
print(r.raw.read(10))
# 将文本流保存到文件
with open(filename, 'wb') as fs:
for chunk in r.iter_content(chunk_size):
fs.write(chunk)
- 定制请求头
import requests
headers = {'user-anget': 'moz'}
r = requests.get('https://api.github.com/some/endpoint', headers=headers)
print(r.text)
- 状态码和cookie
import requests
r = requests.get('https://www.github.com')
print(r.status_code)
print(r.cookies['cookie_name'])
# 发送cookies到服务器,可以使用cookies参数
url = 'https://httpbin.com/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
'''
cookie的返回对象为RequestsCookieJar,他的行为和字典类似,但接口更为完整,适合跨域名,跨路径使用,还可以把Cookie jar传到Requests中
'''
jar = requests.cookies.RequestsCookieJar()
jar.set('cookies_name', 'yum', domain='httpbin.org', path='/cookies')
url = 'https://httpbin.org/cookies'
url = requests.get(url, cookies=jar)
print(r.text)
- 错误和异常
- ConnectionError
- RequestException
- 会话对象
- 会话对象可以让你能够跨请求保持某些参数
import requests
session = requests.Session()
r = session.get('http://www.httpbin.org/cookies/set/sessioncookie/1234567890')
print(r.text)
- 代理
import requests
proxies = {
"http": "http://0127.0.0.1:8090",
"https": "http://10.0.0.1:9000"
}
r = requests.get("https://google.com", proxies=proxies)
print(r.text)
# 若需要使用HTTP Basic Auth,可以使用http://user:pass@host/的方式
proxies = {
"http": "http://user:pass@127.0.0.1:1080/"
}
- httpbasicauth认证
import requests
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth("user@gmail.com", "password")
r = requests.get('https://python.org', auth=auth)
print(r.text)
that's all
Note:后续在补充