0x00 环境简介和安装
我这里使用的是python2.7版本,直接使用pycharm2018这款IDE。
首先在pycharm中配置一下virtualenv环境,virtualenv是一个创建独立Python运行环境的工具,为一个应用创建一套“隔离”的Python运行环境。
创建new project时选择创建新的环境,修改你们自己的目录,如果主机内有多个版本的python解释器可自行选择
创建完成后可以在项目中安装requests库,file-settings打开如下界面
单击右上角绿色加号,搜索requests并install
这里如果你的pip版本大于等于10,安装的时候会报错,原因是由于新版pip的函数发生了变化,解决办法参考这个帖子
【Python】【亲测好用】安装第三方包报错:AttributeError:'module' object has no attribute 'main'
0x01 使用requests
请求方法
-
- GET: 查看资源
- POST: 增加资源
- PUT: 修改资源
- DELETE: 删除资源
- HEAD: 查看响应头
- patch: 局部更新url资源
基本用法:requests.[methon](url)
import requests response = requests.get('https://www.cnblogs.com/Ragd0ll/p/10176258.html') print(response.status_code) # 打印状态码 print(response.url) # 打印请求url print(response.headers) # 打印头信息 print(response.cookies) # 打印cookie信息 print(response.text) #以文本形式打印网页源码 print(response.content) #以字节流形式打印
request的可选参数
1.param
2.data
3.json
4.headers
5.cookies、auth
6.files
7.timeout
8.proxies
9.allow_redirects、stream、verify、cert
带参数的get请求:
第一种直接将参数放在url内
import requests response = requests.get(http://httpbin.org/get?name=gemey&age=22) print(response.text)
第二种是将参数放入字典,然后在请求时给params参数赋值
import requests data = { 'name': 'tom', 'age': 20 } response = requests.get('http://httpbin.org/get', params=data) print(response.text)
两段代码的结果相同
基本POST请求:
import requests data = {'name':'tom','age':'22'} response = requests.post('http://httpbin.org/post', data=data)
简单保存一个二进制文件
import requests response = requests.get('https://img2018.cnblogs.com/blog/1342178/201812/1342178-20181225201042109-1353349536.png') b = response.content with open('F://fengjing.jpg','wb') as f: f.write(b)
为你的请求添加头信息
import requests heads = {} heads['User-Agent'] = 'Mozilla/5.0 ' '(Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 ' '(KHTML, like Gecko) Version/5.1 Safari/534.50' response = requests.get('https://home.cnblogs.com/u/Ragd0ll/',headers=headers)
获取cookie
import requests response = requests.get('https://home.cnblogs.com/u/Ragd0ll/') print(response.cookies) print(type(response.cookies)) for k,v in response.cookies.items(): print(k+':'+v)
会话维持
import requests session = requests.Session() session.get('http://httpbin.org/cookies/set/number/12345') response = session.get('http://httpbin.org/cookies') print(response.text)
证书验证设置
import requests from requests.packages import urllib3 urllib3.disable_warnings() #从urllib3中消除警告 response = requests.get('https://www.12306.cn',verify=False) #证书验证设为FALSE print(response.status_code)
超时异常捕获
import requests from requests.exceptions import ReadTimeout try: res = requests.get('http://httpbin.org', timeout=0.1) print(res.status_code) except ReadTimeout: print(timeout)
异常处理
import requests from requests.exceptions import ReadTimeout,HTTPError,RequestException try: response = requests.get('http://www.baidu.com',timeout=0.5) print(response.status_code) except ReadTimeout: print('timeout') except HTTPError: print('httperror') except RequestException: print('reqerror')
Response的方法