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 在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有 ...
随机推荐
- C#小笔记:单例模式
双重锁定: public class Singleton { private static Singleton instance; private static readonly object syn ...
- WPF ----在UserControl的xaml里绑定依赖属性
场景:在定义wpf 用户控件的时候,希望使用时设置自定义的属性来改变用户控件里的状态或内容等. 下面直接上实例代码: 用户控件的后台代码,定义依赖属性 public partial class MyU ...
- PHPUnit-附录 B. 标注
[http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.annotations.html] 所谓标注,是指某些编程语言中允许加在源代码中的一种特殊格式的语 ...
- nf共享
实验环境是两台Centos6.8 客户端是192.168.3.218 服务端是192.168.3.219 首先配置服务端 1 安装包 用yum安装需要的服务包(两边都安装) yum install n ...
- 使用Python管理数据库
使用Python管理数据库 这篇文章的主题是如何使用Python语言管理数据库,简化日常运维中频繁的.重复度高的任务,为DBA们腾出更多时间来完成更重要的工作.文章本身只提供一种思路,写的不是很全 ...
- Python自动化--语言基础6--模块操作之re、MySQL、Excel
1.Python自有模块正则 import re # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None print(re.match("aaa ...
- Git 上传 GitHub
1.下载 2.安装 3.功能识别 3-1.查看git版本 git --version 3-2.移除原来的版本 yum remove git 4.配置 4-1.用户配置信息 git config ...
- C# Swagger 生成接口文档
一直听说Swagger是做Web API文档的好工具,这次手里暂时没什么事,类体验下它的强大之处.下面是使用Swashbuckle.net 给asp.net web API添加文档的简要步骤. 参考地 ...
- 使用docker搭建wordpress
前言 去年在学习docker,在看完菜鸟教程和第一本docker书后,一直想实战用一下这个技术,多用用才能熟能生巧,真正体验它的利弊.正好傅老板用docker搭完了wordpress,我也就手痒跟着搭 ...
- 创建Maven web工程不能解析EL表达式的解决办法
在web.xml中讲头部改为: <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee&qu ...