Request库
Requests is an elegant and simple HTTP library for Python, built for human beings.
Request库是日常中经常使用的库,总结一下吧。附上文档Requests: 让 HTTP 服务人类
发送请求
#使用requests库前都要导入requests库
import requests
#发送GET,POST,PUT,DELETE,HEAD 以及 OPTIONS 请求
r = requests.get('https://www.cnblogs.com/thenbz3/')
r = requests.post('https://www.cnblogs.com/thenbz3/')
r = requests.put('https://www.cnblogs.com/thenbz3/')
r = requests.delete('https://www.cnblogs.com/thenbz3/')
r = requests.head('https://www.cnblogs.com/thenbz3/')
r = requests.options('https://www.cnblogs.com/thenbz3/')
传递URL参数
#GET请求时传递参数
payload = {'key1':'value1', 'key2':'value2', 'key3' : ['value3','valua4']}
r = requests.get('http://httpbin.org/get', params = payload)
print(r.url)
#相当于
r = requests.get('http://httpbin.org/get?key1=value1&key2=value2&key3=value3&key3=value4')
更加复杂的POST请求
#POST请求时传递参数
import requests
payload = {'key1' : 'value1', 'key2' : 'value2'}
r = requests.post('http://httpbin.org/post', data = payload)
#或者用元组列表
paylaod = (('key1', 'value1'),('key1', 'value2'))
r = requests.post('http://httpbin.org/post', data = payload)
响应内容
#以GET请求举例,会以html代码形式返回页面的内容
import requests
url = 'https://www.cnblogs.com/thenbz3/'
r = request.get(url)
html = t.text
print(html)
print(r.encoding)
#还可以更改返回页面内容的编码
r.encoding = 'utf-8'
print(r.encoding)
# 响应状态码
r.status_code
r.status_code == requests.codes.ok #内置状态码查询对象
r.raise_for_status() #抛出异常,无异常返回None
print(r)
定制请求头
import requests
url = 'https://www.cnblogs.com/thenbz3/'
headers = {'user-agent' : 'my-app/o.o1'}
r=request.get(url,headers = headers)
Cookie
r.cookies['cookie的键名']
#发送请求时带cookie
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
#Cookie Jar
jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
url = 'http://httpbin.org/cookies'
r = requests.get(url, cookies=jar)
r.text
'{"cookies": {"tasty_cookie": "yum"}}'
重定向
#allow_redirects=False 禁用重定向
r = requests.get('http://github.com', allow_redirects=False)
会话对象Session
#会保持同一个cookie,和某些参数,在编写脚本的时候如果没有特殊要求,最好用session
import requests
s = requests.session()
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")
print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'
#不过需要注意,就算使用了会话,方法级别的参数也不会被跨请求保持。下面的例子只会和第一个请求发送 cookie ,而非第二个
r = s.get('http://httpbin.org/cookies', cookies={'from-my': 'browser'})
print(r.text)
# '{"cookies": {"from-my": "browser"}}'
r = s.get('http://httpbin.org/cookies')
print(r.text)
# '{"cookies": {}}'