requests库是一个流行的用于发送Http请求的Python第三方库, 其设计简洁高效可以完美替代默认的urllib。
使用pip安装requests:
pip install requests
引入模块:
import requests
发送GET请求:
response = requests.get(url)
在response对象中可以得到响应的相关信息。
>>> response = requests.get('http://www.cnblogs.com')
>>> response = requests.get('http://www.cnblogs.com/Finley/')
>>> >>> response.status_code
200
>>> response.text
<!DOCTYPE html>
<html>
...
>>> response.raw # 获得原始响应
<requests.packages.urllib3.response.HTTPResponse object at 0x10e3b3490>
>>> response.request # 获得请求对象
<PreparedRequest [GET]>
发送其它方法的请求:
response = requests.post(url)
response = requests.head(url)
response = requests.delete(url)
response = requests.put(url)
response = requests.options(url)
传递url参数:
response = requests.get(url, params={key:val})
查看已经编码的url: response.url
添加请求头:
response = requests.get(url, headers={key: val})
查看请求头和响应头:
>>> response.headers
{'Content-Encoding': 'gzip',
'Transfer-Encoding': 'chunked',
'Expires': 'Mon, 21 Nov 2016 09:01:00 GMT',
'Vary': 'Accept-Encoding',
'Last-Modified': 'Mon, 21 Nov 2016 09:00:50 GMT',
'Connection': 'keep-alive',
'X-UA-Compatible': 'IE=10',
'Cache-Control':
'private, max-age=10',
'Date': 'Mon, 21 Nov 2016 09:00:50 GMT',
'Content-Type': 'text/html; charset=utf-8'
}
>>> response.request.headers
{'Connection': 'keep-alive',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'User-Agent': 'python-requests/2.11.1'
}
模拟提交表单:
response = requests.post(url, data={key: val})
可以使用multipart-encoded上传文件:
files = {'file': open(path, 'rb')}
response = requests.post(url, files=files)
也可以设置文件名和请求头:
files = {
'file': (filename, open(path, 'rb')),
header_name: header_val
}
response = requests.post(url, files=files)
添加cookies:
response = requests.post(url, cookies={key: val})
查看cookies:
>>> response.cookies
<RequestsCookieJar[]>
requests只能提供阻塞IO, 使用timeout以秒为单位设置响应超时时间:
response = requests.post(url, timeout=0.2)