requests库使用
介绍:
发送HTTP请求的第三方库,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3)
安装:pip3 install requests
学习requests前,可以先熟悉下HTTP协议
http://www.cnblogs.com/linhaifeng/p/6266327.html
GET请求:
import requests
from urllib import parse
param={'wd':'中国'}
# 对url进行传参
response = requests.get('http://www.baidu.com/s?', params=param)
print(response.url)
# url解码 ASCII --》utf8
print(parse.unquote(response.url)) >>输出
http://www.baidu.com/s?wd=%E4%B8%AD%E5%9B%BD
http://www.baidu.com/s?wd=中国
GET请求->headers
通常我们在发送请求时都需要带上请求头,请求头是将自身伪装成浏览器的关键
# 添加headers(浏览器会识别请求头,不加可能会被拒绝访问,比如访问https://www.zhihu.com/explore)
import requests
response = requests.get('https://www.zhihu.com/explore')
print(response.status_code) # 返回500错误 # 自己定制headers
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
}
respone = requests.get('https://www.zhihu.com/explore',headers = headers)
print(respone.status_code) # 返回200
GET请求->cookies
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
}
loginUrl = 'https://github.com/login'
# 获取cookies
cookies = response.cookies
print('cookies=>',cookies)
GET请求->代理
import requests
proxies={
'http':'111.47.220.67:8080',
'https':'111.47.220.67:8080',
}
response = requests.get('https://www.zhihu.com/explore',
proxies= proxies,headers = headers, verify=False) print(response.status_code)
GET请求->超时设置
import requests #timeout代表接收数据的超时时间
response = requests.get('https://www.baidu.com', timeout=1)
print(response.status_code)
response
response属性
import requests
response = requests.get('http://www.jianshu.com')
# response属性
print(response.text) # 文本数据str 经过转码的
print(response.content) # 原始数据字节串bytes
print(response.status_code) # 返回状态码 200
print(response.headers)
print(response.cookies)
print(response.cookies.get_dict())
print(response.cookies.items())
print(response.url)
print(response.history)
print(response.encoding)
编码问题
# 编码问题
import requests
response = requests.get('http://www.autohome.com/news')
print(response.headers['Content-Type']) # 返回text/html
# 如果返回值不包括charset元素,默认返回编码为ISO-8859-1
print(response.encoding) # 返回ISO-8859-1 按ISO-8859-1方式解码text
response.encoding = 'GBK' # 汽车之家网站返回的页面内容为gb2312编码的,而requests的默认编码为ISO-8859-1,如果不设置成gbk则中文乱码
print(response.text) response = requests.get('https://www.jianshu.com')
print(response.headers['Content-Type']) # 返回text/html; charset=utf-8
# 返回值包括charset元素,返回编码为charset后的编码
print(response.encoding) # 返回utf-8 ,按utf-8方式解码text
print(response.text) # 简书返回的页面内容为utf-8编码的,在这里不用设置response.encoding = 'utf-8'
解析json
# 解析json
import requests
import json
response = requests.get('http://httpbin.org/get')
res1 = json.loads(response.text) # 以往获取方式太麻烦
res2 = response.json() # 直接获取json数据
print(res2)
print(res1 == res2) # True
获取二进制数据
import requests
response = requests.get('http://pic-bucket.nosdn.127.net/photo/0005/2018-02-26/DBIGGI954TM10005NOS.jpg')
with open('a.jpg', 'wb') as f:
f.write(response.content) # stream参数:一点一点的取,比如下载视频时,如果视频100G,用response.content然后一下子写到文件中是不合理的
response = requests.get('https://gss3.baidu.com/6LZ0ej3k1Qd3ote6lo7D0j9wehsv/tieba-smallvideo-transcode/1767502_56ec685f9c7ec542eeaf6eac93a65dc7_6fe25cd1347c_3.mp4',
stream = True) with open('b.mp4','wb') as f:
# 获取二进制流(iter_content)
for line in response.iter_content():
f.write(line)
基于POST请求
1、介绍
#GET请求
HTTP默认的请求方法就是GET
* 没有请求体
* 数据必须在1K之内!
* GET请求数据会暴露在浏览器的地址栏中 GET请求常用的操作:
1. 在浏览器的地址栏中直接给出URL,那么就一定是GET请求
2. 点击页面上的超链接也一定是GET请求
3. 提交表单时,表单默认使用GET请求,但可以设置为POST #POST请求
(1). 数据不会出现在地址栏中
(2). 数据的大小没有上限
(3). 有请求体
(4). 请求体中如果存在中文,会使用URL编码! #!!!requests.post()用法与requests.get()完全一致,特殊的是requests.post()有一个data参数,用来存放请求体数据
2、发送post请求,登录github
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/2/27 20:42
# @Author : hyang
# @Site :
# @File : request_github.py
# @Software: PyCharm import re
import requests
import http.cookiejar as cookielib
from requests.packages import urllib3 '''
一 目标站点分析
浏览器输入https://github.com/login
然后输入错误的账号密码,通过Fiddle抓包
发现登录行为是post提交到:https://github.com/session
而且请求头包含cookie
而且请求体包含:
commit:Sign in
utf8:✓
authenticity_token:lbI8IJCwGslZS8qJPnof5e7ZkCoSoMn6jmDTsL1r/m06NLyIbw7vCrpwrFAPzHMep3Tmf/TSJVoXWrvDZaVwxQ==
login:908099665@qq.com
password:123 二 流程分析
先GET:https://github.com/login拿到初始cookie与authenticity_token
返回POST:https://github.com/session, 带上初始cookie,带上请求体(authenticity_token,用户名,密码等)
最后拿到登录cookie ps:如果密码时密文形式,则可以先输错账号,输对密码,然后到浏览器中拿到加密后的密码,github的密码是明文
'''
import ssl
# 解决某些环境下报<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
ssl._create_default_https_context = ssl._create_unverified_context
urllib3.disable_warnings() # 关闭警告 headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
}
loginUrl = 'https://github.com/login'
postUrl = 'https://github.com/session' response = requests.get(loginUrl, headers=headers, verify=False)
# 获取authenticity_token
authenticity_token = re.findall(r'<input name="authenticity_token" type="hidden" value="(.*?)" />', response.text)
# 获取cookies
cookies = response.cookies
print('cookies=>',cookies)
print('authenticity_token=>',authenticity_token) email='908099665@qq.com'
password='yanghaoXXXX'
post_data={
"commit":"Sign in",
"utf8":"✓",
"authenticity_token":authenticity_token,
"login":email,
"password":password,
}
response2 = requests.post(postUrl, data=post_data, headers=headers, verify=False, cookies=cookies)
print(response2.status_code)
print(response2.history) # 跳转的历史状态码
print(response2.text)
分析抓包


3. session的使用
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/2/26 23:31
# @Author : hyang
# @Site :
# @File : request-github.py
# @Software: PyCharm import re
import requests
import urllib3
import http.cookiejar as cookielib import ssl
# 解决某些环境下报<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
ssl._create_default_https_context = ssl._create_unverified_context
urllib3.disable_warnings() # 关闭警告 headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
} loginUrl = 'https://github.com/login'
postUrl = 'https://github.com/session'
profileUrl = 'https://github.com/settings/emails'
session = requests.session() # 包括了cookies信息
# 生成 github_cookie文件
session.cookies = cookielib.LWPCookieJar(filename='github_cookie') # 获取authenticity_token
def get_token():
response = session.get(loginUrl, headers=headers, verify=False)
html = response.text
authenticity_token = re.findall(r'<input name="authenticity_token" type="hidden" value="(.*?)" />', html)
print(authenticity_token)
return authenticity_token # 登陆表单提交参数
def post_account(email, password):
post_data = {
'commit': 'Sign in',
'utf8':'✓',
'authenticity_token': get_token(),
'login': email,
'password': password
}
response = session.post(postUrl, data=post_data, headers=headers)
print(response.status_code)
# 保存cookies
session.cookies.save() def load_cookie():
try:
session.cookies.load(ignore_discard=True)
print('cookie 获取成功')
except:
print('cookie 获取不成功') # 判断是否登陆成功
def isLogin():
load_cookie()
response = session.get(profileUrl, headers=headers)
#print('908099665@qq.com' in response.text)
return '908099665@qq.com' in response.text if __name__ == "__main__":
# 输入自己email账号和密码
post_account(email='908099665@qq.com', password='yanghaoXXXX')
# 验证是否登陆成功
isLogin()
重定向
By default Requests will perform location redirection for all verbs except HEAD.
We can use the history property of the Response object to track redirection.
The Response.history list contains the Response objects that were created in order to complete the request. The list is sorted from the oldest to the most recent response.
For example, GitHub redirects all HTTP requests to HTTPS:
>>> r = requests.get('http://github.com')
>>> r.url
'https://github.com/'
>>> r.status_code
>>> r.history
[<Response [301]>]
If you're using GET, OPTIONS, POST, PUT, PATCH or DELETE, you can disable redirection handling with the allow_redirects parameter:
>>> r = requests.get('http://github.com', allow_redirects=False)
>>> r.status_code
>>> r.history
[]
If you're using HEAD, you can enable redirection as well:
>>> r = requests.head('http://github.com', allow_redirects=True)
>>> r.url
'https://github.com/'
>>> r.history
[<Response [301]>]
先看官网的解释
高级认证
#证书验证(大部分网站都是https)
import requests
respone=requests.get('https://www.12306.cn') #如果是ssl请求,首先检查证书是否合法,不合法则报错 #改进1:去掉报错,但是会报警告
import requests
respone=requests.get('https://www.12306.cn',verify=False) #不验证证书,报警告,返回200
print(respone.status_code) #改进2:去掉报错,并且去掉警报信息
import requests
from requests.packages import urllib3
urllib3.disable_warnings() #关闭警告
respone=requests.get('https://www.12306.cn',verify=False)
print(respone.status_code) #改进3:加上证书
#很多网站都是https,但是不用证书也可以访问,大多数情况都是可以携带也可以不携带证书
#知乎\百度等都是可带可不带
#有硬性要求的,则必须带,比如对于定向的用户,拿到证书后才有权限访问某个特定网站
import requests
respone=requests.get('https://www.12306.cn',
cert=('/path/server.crt',
'/path/key'))
print(respone.status_code)
文件上传
import requests
files = {'file':open('a.pptx','rb')}
respone = requests.post('http://httpbin.org/post',files=files)
print(respone.status_code)
异常处理
# 异常处理
import requests
from requests.exceptions import * # 可以查看requests.exceptions获取异常类型 try:
r = requests.get('http://www.baiduxxx.com', timeout=1)
except ReadTimeout:
print('ReadTimeout')
except ConnectionError: # 网络不通
print('ConnectionError')
# except Timeout:
# print('aaaaa')
except RequestException:
print('Error')
requests库使用的更多相关文章
- Python爬虫小白入门(二)requests库
一.前言 为什么要先说Requests库呢,因为这是个功能很强大的网络请求库,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据.网络上的模块.库.包指的都是同一种东西,所以后文中可能会在不同地 ...
- Requests库上传文件时UnicodeDecodeError: 'ascii' codec can't decode byte错误解析
在使用Request上传文件的时候碰到如下错误提示: 2013-12-20 20:51:09,235 __main__ ERROR 'ascii' codec can't decode byte 0x ...
- Requests库的几种请求 - 通过API操作Github
本文内容来源:https://www.dataquest.io/mission/117/working-with-apis 本文的数据来源:https://en.wikipedia.org/wiki/ ...
- python脚本实例002- 利用requests库实现应用登录
#! /usr/bin/python # coding:utf-8 #导入requests库 import requests #获取会话 s = requests.session() #创建登录数据 ...
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...
- python WEB接口自动化测试之requests库详解
由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...
- python爬虫从入门到放弃(四)之 Requests库的基本使用
什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现,其 ...
- (转)Python爬虫利器一之Requests库的用法
官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...
- python requests库学习笔记(上)
尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...
- 使用Python的requests库进行接口测试——session对象的妙用
from:http://blog.csdn.net/liuchunming033/article/details/48131051 在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有 ...
随机推荐
- 通过四个例子理解JavaScript拓展运算符
原文地址:JavaScript & The spread operator 拓展运算符看起来像什么? 三个点,... 它能做什么? 拓展运算符允许一个表达式在某个地方展开成为多个元素.变量或参 ...
- Thrift入门
简介 Thrift最初由Facebook研发,主要用于各个服务之间的RPC通信,支持跨语言,常用的语言比如C++, Java, Python, PHP, Ruby, Erlang, Perl, Has ...
- Scrapy-简单介绍
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 其最初是为了 页面抓取 (更确切来说, 网络抓取 )所设计的, ...
- 洛谷P1962 斐波那契数列【矩阵运算】
洛谷P1962 斐波那契数列[矩阵运算] 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) ( ...
- ob_start、ob_get_contents使用
要善于ob_start.ob_get_contents使用, 比如需要ajax获取 请求页面的模板信息 这个时候用到就很便捷 ob_start(); $this->assign('config' ...
- ci框架基础知识点
一.路由 1.index.php/test/hello->控制器test的hello方法 2. 也可以手动配置路由 app/config/routes.php中 I:$route[' ...
- jQuery学习心得一
1.闭包 每次看到jQuery的时候,首先想到的就是闭包.这是个老生常谈的问题了.今天重新回忆了一下闭包. 什么是闭包? 当有一个函数想要访问另一个函数内部的变量,这个是访问不了的.所有我们要用闭包来 ...
- 2.数码相框-编码(ASCII/GB2312/Unicode)介绍,并使LCD显示汉字字符(2)
在上章-学习了数码相框的框架分析(1)了 本章主要内容如下: 1)熟悉ASCII/GB2312/Unicode编码 2)写应用程序,使LCD显示汉字和字符 大家都知道,数据传输的是二进制,而字符和汉字 ...
- HDU - 3038 种类并查集
思路:种类并查集的每个节点应该保存它的父节点以及他和父节点之间的关系.假设root表示根结点,sum[i-1]表示i到根结点的和,那么sum[j-1] - sum[i]可以得到区间[j, i]的和.那 ...
- Java Enum总结
枚举类型 枚举类型是Java5新增的特性之一,枚举是一种特殊类型的类,其枚举的每一个值 都是该枚举类的一个实例.枚举类型是作为Java语言的一部分,是完全类型安全的, 编译器会帮助我们检查枚举类型的正 ...