01: requests模块
目录:
1.1 requests模块简介返回顶部
1. requests模块介绍
1、 Python标准库中提供了:urllib、urllib2、httplib等模块以供Http请求,但是,它的 API 太渣了。
2、 Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装
3、 从而使得Pythoner进行网络请求时,变得美好了许多,使用Requests可以轻而易举的完成浏览器可有的任何操作
2. requests请求头和请求体
1、发送http请求包含请求头和请求体两部分,请求头和请求体使用”\r\n\r\n”分隔,请求头和请求头之间用一个
‘\r\n’,进行分隔,get请求只有请求头没有请求体,post请求同时有请求头和请求体
2、发送http请求时如果涉及cookies,cookies是放到请求头中,如果是回写cookie时是将cookie放到响应头中回写的cookie一般名字就叫(set-cookie)
3、如果有重定向
- 响应头中有一个location参数
3. requests模块常用方法
1、 pip install requests
2、 response = requests.get('http://www.baidu.com/ ') #获取指定url的网页内容
3、 response.text #获取文本文件
4、 response.content #获取字节类型
5、 response.encoding = ‘utf-8’ #指定获取的网页内容用utf-8编码
response.encoding = response.apparent_encoding #下载的页面是什么编码就用什么编码格式
6、 response.cookies #拿到cookies
response.cookies.get_dict() #拿到cookie字典样式
1.2 使用requests模块发送get请求返回顶部
1、requests发送get请求的常用参数
import requests
ret = requests.get('https://github.com/timeline.json') print(ret.url) # 打印访问的url
print(ret.text) # 打印返回值
get发送无参数实例
import requests payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.get("http://httpbin.org/get", params=payload) print(ret.url) # 打印访问的url http://httpbin.org/get?key1=value1&key2=value2
print(ret.text) # 打印返回值
get发送有参数实例
import requests
ret = requests.get(
url='http://www.baidu.com',
params={'k1':123,'k2':456}, #http://www.baidu.com?k1=123&k2=456
cookies={'c1':'','c2':''}, #requests会将这个cookie放到请求头中
headers={ #一般在请求头中做爬虫限制就下面三个限制
'User-Agent':'', # 伪造浏览器标记
'Referer': 'http://dig.chouti.com/',
# 有些网站在爬取时做了Referer限制,即判断上一次访问的是否是这个网站,是才让登录
'xml-httprequest':'true', #发送ajax请求可能就会有这个标记
}
)
print ret.text
requests发送get请求常用参数
2、Requests一些其他参数
1、 auth参数:帮我们上传的用户名和密码进行简单加密
说明:有些网页登录时没有HTML页面,直接就是一个弹窗框,此时即可用这种方法发送登录请求
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
ret = requests.get('https://api.github.com/user', auth=HTTPBasicAuth('wupeiqi', 'abc'))
auth参数
2、timeout参数:请求多久未响应就超时
ret = requests.get('http://google.com/', timeout=1)
timeout参数
3、allow_redirects参数:是否允许重定向
ret = requests.get('http://127.0.0.1:8000/test/', allow_redirects=False)
allow_redirects参数
4、 proxies参数:代理(需要有代理服务器)
import requests proxies = {'http://10.20.1.128': 'http://10.10.1.10:5323'} ret = requests.get("http://www.proxy360.cn/Proxy", proxies=proxies)
print(ret.headers) from requests.auth import HTTPProxyAuth proxyDict = {'http': '77.75.105.165','https': '77.75.105.165'}
auth = HTTPProxyAuth('username', 'mypassword') r = requests.get("http://www.google.com", proxies=proxyDict, auth=auth)
print(r.text)
proxies参数
5、stream参数:一点点下载,一点点保存(比如内存很小,无法直接下载大文件)
ret = requests.get('http://127.0.0.1:8000/test/', stream=True)
print(ret.content)
ret.close()
stream参数
6、verify参数:忽略证书直接访问https网页
requests.get(
url='https:xxxx',
verify = False, #忽略证书
# cert='fuck.pem', #自己制作的证书
# cert=('funck.crt','xxx.key') #花钱买的第三方可信赖的证书(这种证书已经植入到操作系统中了)
)
verify参数
1.3 使用requests模块发送post请求返回顶部
1.基本POST实例
import requests payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.post("http://httpbin.org/post", data=payload) print(ret.text)
基本POST实例
2. 发送请求头和数据实例
import requests
import json url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'} ret = requests.post(url, data=json.dumps(payload), headers=headers) print(ret.text)
print(ret.cookies)
发送请求头和数据实例
3. requests发送post请求常用参数参数
requests.request(
method='POST', # 提交方式
url='http://www.oldboyedu.com', # 提交地址
data={'user': 'alex', 'pwd': ''}, # 通过请求体传递数据:post方式
# json = {'user':'alex','pwd':'123',{'k1':'v1','k2':'v2'}},
# json和data都是通过post向请求体传递数据,但是json传递的数据可以在字典中嵌套字典
cookies={'cook1': 'value1'}, # 发送Cookie到服务器端
headers={
'Referer': 'http://dig.chouti.com/',
# 有些网站在爬取时做了Referer限制,即判断上一次访问的是否是这个网站,是才让登录
'User-Agent': 'Mozilla/5.0Safari/537.36', # 伪造浏览器客户端(这里是谷歌浏览器)
},
)
requests发送post请求常用参数参数
4. requests.post发送文件
import requests
requests.post(
url='xxx',
files={
'f1':open('s1.py','rb'), #这样就可以将s1.py这个文件上传到上面url中了
'f2':('ssss1.py',open('s1.py','rb')),
#指定上传文件名:第一个参数是上传到服务器端的文件名
}
)
requests.post发送文件
1.4 requests.request()参数介绍返回顶部
1. requests.request()介绍
1、上面使用的requests.post()和requests.get()实际上就是调用requests.request()方法,只是传递的参数不同
2. requests.request()常用的几个参数(cookies是在headers中传递过去的)
requests.request(
method='POST', # 提交方式
url='http://www.oldboyedu.com', # 提交地址
params={'k1': 'v1', 'k2': 'v2'}, # 在url中传递的数据:get方式
data={'user': 'alex', 'pwd': ''}, # 通过请求体传递数据:post方式
# json = {'user':'alex','pwd':'123',{'k1':'v1','k2':'v2'}},
# json和data都是通过post向请求体传递数据,但是json传递的数据可以在字典中嵌套字典
cookies={'cook1': 'value1'}, # 发送Cookie到服务器端
headers={
'Referer': 'http://dig.chouti.com/',
# 有些网站在爬取时做了Referer限制,即判断上一次访问的是否是这个网站,是才让登录
'User-Agent': 'Mozilla/5.0Safari/537.36', # 伪造浏览器客户端(这里是谷歌浏览器)
},
)
requests.request()常用的几个参数
3、requests.Session()帮我们自动找到cookie携带信息自动登录
import requests
session = requests.Session()
### 1、首先登陆任何页面,获取cookie
i1 = session.get(url="http://dig.chouti.com/help/service")
### 2、用户登陆,携带上一次的cookie,后台对cookie中的 gpsd 进行授权
i2 = session.post(
url="http://dig.chouti.com/login",
data={
'phone': "",
'password': "7481079xl",
'oneMonth': ""
})
### 3、这个是点赞某条新闻的url(这样就可以模仿登录点赞了)
i3 = session.post(url="http://dig.chouti.com/link/vote?linksId=15055231",)
requests.Session()帮我们自动找到cookie携带信息自动登录
4、发送请求与传递参数其他方式
requests.get(‘https://github.com/timeline.json’) # GET (从服务器取出资源)
requests.post(“http://httpbin.org/post”) # POST (在服务器新建一个资源)
requests.put(“http://httpbin.org/put”) # PUT (在服务器更新资源:客户端提供改变后的完整资源)
requests.delete(“http://httpbin.org/delete”) # DELETE (从服务器删除资源)
requests.head(“http://httpbin.org/get”) # HEAD
requests.options(“http://httpbin.org/get”) # OPTIONS
01: requests模块的更多相关文章
- 洗礼灵魂,修炼python(61)--爬虫篇—【转载】requests模块
requests 1.简介 Requests 是用Python语言编写的第三方库,所以你需要pip安装,安装过程就略过了.它基于urllib,采用 Apache2 Licensed 开源协议的 HTT ...
- 给requests模块添加请求头列表和代理ip列表
Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,符合了Python语言的思想,通俗的说去繁存 ...
- requests模块基础
requests模块 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bor ...
- python网络爬虫之二requests模块
requests http请求库 requests是基于python内置的urllib3来编写的,它比urllib更加方便,特别是在添加headers, post请求,以及cookies的设置上,处理 ...
- 模块讲解---time模块,datetime模块,random模块,hashlib模块和hmac模块,typing模块,requests模块,re模块
目录 1. 包 2. time模块 1. 优先掌握 2. 了解 3. datetime模块 1. 优先掌握 4. random模块 1. 优先掌握 2. 了解 5. hashlib模块 ...
- 爬虫(2) - Requests(1) | Requests模块的深度解析
1.Requests 安装与请求方法 requests官方文档:https://docs.python-requests.org/zh_CN/latest/,官方文档不知道为什么挂了,访问不了.我找了 ...
- 爬虫requests模块 1
让我们从一些简单的示例开始吧. 发送请求¶ 使用 Requests 发送网络请求非常简单. 一开始要导入 Requests 模块: >>> import requests 然后,尝试 ...
- requests 模块
发送请求 使用Requests发送网络请求非常简单. 一开始要导入Requests模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取Gith ...
- requests模块--python发送http请求
requests模块 在Python内置模块(urllib.urllib2.httplib)的基础上进行了高度的封装,从而使得Pythoner更好的进行http请求,使用Requests可以轻而易举的 ...
随机推荐
- 2016江苏省CPC省赛 I - Itinerary Planning
Description Mike moved to a new city. There are bus stations in the city, each has a unique name. Ea ...
- ORACLE中Drop table cascade constraints之后果.
当你要drop一个table时,如果删除table的动作会造成trigger或constraint产生矛盾,系统会出现错误警告的讯息而不会允许执行..一个极简单的例子,例如你有一个员工基本资料表,上面 ...
- garbage collection - 垃圾收集 生命周期 跟踪内存使用
Professional.JavaScript.for.Web.Developers.3rd.Edition.Jan.2012 JavaScript is a garbage-collected la ...
- python 基础 内置函数
内置参数 print(all([5,-1,5])) # 非0都是真 true print(all([0,-1,5])) # false print(any([1,0,5])) # 有一个数据为真,就为 ...
- iOS入门怎样选择Swift和objective-c
版权声明:本文为博主原创文章,未经博主同意不得转载.博主微信:lofocus https://blog.csdn.net/cuibo1123/article/details/28261795 学oc吧 ...
- literallycanvas的简介
LiterallyCanvas是什么 Literally Canvas是一个可扩展的开源(BSD许可)HTML5绘图组件,可以用于网页中插入画图板,类似于windows自带的画图板.可以用可视化工具绘 ...
- java-信息安全(八)-迪菲-赫尔曼(DH)密钥交换【不推荐,推荐Oakley】
概述 信息安全基本概念: DH(Diffie–Hellman key exchange,迪菲-赫尔曼密钥交换) DH 是一种安全协议,,一种确保共享KEY安全穿越不安全网络的方法,它是OAKLEY的一 ...
- 使用PHP创建一个REST API(译)
最近API在网络领域有些风靡,明确的说是REST的影响力.这实在没什么好惊讶的,因为在任何编程语言中,消费REST API都是非常的容易.构建它也非常的简单,因为本质上你不会用到任何那些已存在很久的H ...
- requesMapping注解
java类 package org.springframework.web.bind.annotation; import java.lang.annotation.Documented; impor ...
- Scala系统学习(一):Scala概述
Scala是可扩展语言的缩写,是一种混合功能编程语言. 它由Martin Odersky创建. Scala顺利整合面向对象和函数式语言的功能. Scala被编译后在Java虚拟机上运行. 许多现有公司 ...