虽然Python的标准库中 urllib模块已经包含了日常咱们使用的大多数功能,可是它的 API 使用起来让人感受不太好,而 Requests宣传是 “HTTP for Humans”,说明使用更简洁方便。html
利用pip
能够很是方便的安装:python
pip install requests
中文文档:http://docs.python-requests.org/zh_CN/latest/index.html
github地址:https://github.com/requests/requestsgit
最简单的发送get
请求就是经过requests.get
来调用:github
response = requests.get("http://www.baidu.com/")
添加headers和查询参数:
若是想添加 headers,能够传入headers参数来增长请求头中的headers信息。若是要将参数放在url中传递,能够利用 params 参数。相关示例代码以下:web
import requests kw = {'wd':'中国'} headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"} # params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不须要urlencode() response = requests.get("http://www.baidu.com/s", params = kw, headers = headers) # 查看响应内容,response.text 返回的是Unicode格式的数据 print(response.text) # 查看响应内容,response.content返回的字节流数据 print(response.content) # 查看完整url地址 print(response.url) # 查看响应头部字符编码 print(response.encoding) # 查看响应码 print(response.status_code)
最基本的POST请求能够使用post
方法:json
response = requests.post("http://www.baidu.com/",data=data)
传入data数据:
这时候就不要再使用urlencode
进行编码了,直接传入一个字典进去就能够了。好比请求拉勾网的数据的代码:cookie
import requests url = "https://www.lagou.com/jobs/positionAjax.json?city=%E6%B7%B1%E5%9C%B3&needAddtionalResult=false&isSchoolJob=0" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36', 'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=' } data = { 'first': 'true', 'pn': 1, 'kd': 'python' } resp = requests.post(url,headers=headers,data=data) # 若是是json数据,直接能够调用json方法 print(resp.json())
使用requests
添加代理也很是简单,只要在请求的方法中(好比get
或者post
)传递proxies
参数就能够了。示例代码以下:session
import requests url = "http://httpbin.org/get" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36', } proxy = { 'http': '171.14.209.180:27829' } resp = requests.get(url,headers=headers,proxies=proxy) with open('xx.html','w',encoding='utf-8') as fp: fp.write(resp.text)
若是在一个响应中包含了cookie
,那么能够利用cookies
属性拿到这个返回的cookie
值:ide
import requests url = "http://www.renren.com/PLogin.do" data = {"email":"970138074@qq.com",'password':"pythonspider"} resp = requests.get('http://www.baidu.com/') print(resp.cookies) print(resp.cookies.get_dict())
以前使用urllib
库,是能够使用opener
发送多个请求,多个请求之间是能够共享cookie
的。那么若是使用requests
,也要达到共享cookie
的目的,那么能够使用requests
库给咱们提供的session
对象。注意,这里的session
不是web开发中的那个session,这个地方只是一个会话的对象而已。仍是以登陆人人网为例,使用requests
来实现。示例代码以下:post
import requests url = "http://www.renren.com/PLogin.do" data = {"email":"970138074@qq.com",'password':"pythonspider"} headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" } # 登陆 session = requests.session() session.post(url,data=data,headers=headers) # 访问大鹏我的中心 resp = session.get('http://www.renren.com/880151247/profile') print(resp.text)
对于那些已经被信任的SSL整数的网站,好比https://www.baidu.com/
,那么使用requests
直接就能够正常的返回响应。示例代码以下:
resp = requests.get('http://www.12306.cn/mormhweb/',verify=False)
print(resp.content.decode('utf-8'))