request库
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':''}
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的方法

request库的更多相关文章
- Python3 urllib.request库的基本使用
Python3 urllib.request库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urlli ...
- Python request库与爬虫框架
Requests库的7个主要方法 requests.request():构造一个请求,支持以下各方法的基础方法 requests.get():获取HTML网页的主要方法,对应于HTTP的GET ...
- Request库使用response.text返回乱码问题
我们日常使用Request库获取response.text,这种调用方式返回的text通常会有乱码显示: import requests res = requests.get("https: ...
- 爬虫——urllib.request库的基本使用
所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地.在Python中有很多库可以用来抓取网页,我们先学习urllib.request.(在python2.x中为urllib2 ...
- 爬虫request库规则与实例
Request库的7个主要方法: requests.request(method,url,**kwargs) method:请求方式,对应get/put/post等7种: r = reques ...
- Python网络爬虫与信息提取[request库的应用](单元一)
---恢复内容开始--- 注:学习中国大学mooc 嵩天课程 的学习笔记 request的七个主要方法 request.request() 构造一个请求用以支撑其他基本方法 request.get(u ...
- Request库的安装与使用
Request库的安装与使用 安装 pip install reqeusts Requests库的7个主要使用方法 requests.request() 构造一个请求,支撑以下各方法的基础方法 req ...
- python网络爬虫学习笔记(一)Request库
一.Requests库的基本说明 引入Rquests库的代码如下 import requests 库中支持REQUEST, GET, HEAD, POST, PUT, PATCH, DELETE共7个 ...
- Request库学习
0x00前言 这库让我爱上了python 碉堡! 开心去学了一些python,然后就来学这个时候神库~~ 资料来源:http://cn.python-requests.org/en/latest/u ...
- 爬虫入门【1】urllib.request库用法简介
urlopen方法 打开指定的URL urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, ca ...
随机推荐
- PHP 计算代码运行所占内存和时间
PHP 计算代码运行所占内存和时间 在PHP开发过程中,写出高质量的代码是很重要的,除了代码必须规范之外,性能也是不可忽视的一方面,那么如果检验一段代码是否高效呢,可通过以下一段php代码来粗略检测 ...
- ZendFramework-2.4 源代码 - 关于MVC - Model层类图
- 经典dfs(depth-first search)
DFS主要在于参数的改变; 样例输入: n=4 //给定n个数字 a={1,2,4,7} //输入n个数据 k=15 //目标数字 样例输 ...
- wusir 面试题答案在老男孩的视频里
注意:你问答案在哪里?答案在视频里了,就是不给你写. 第一部分 Python基础篇(80题) 为什么学习Python? 通过什么途径学习的Python? Python和Java.PHP.C.C#.C+ ...
- ubuntu 把软件源修改为国内源和更新(转载)
1. 备份原始文件 sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup 2. 修改文件并添加国内源 vi /etc/apt/sourc ...
- Building a Space Station POJ - 2031
Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...
- luogu3810 【模板】三维偏序(陌上花开)
ref1 ref2 ref3 ref4 #include <algorithm> #include <iostream> #include <cstdio> usi ...
- 【Minimum Path Sum】cpp
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- leetcode 【Search a 2D Matrix 】python 实现
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- Mysql Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table ...