目录:

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参数

    3allow_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参数

    6verify参数:忽略证书直接访问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模块的更多相关文章

  1. 洗礼灵魂,修炼python(61)--爬虫篇—【转载】requests模块

    requests 1.简介 Requests 是用Python语言编写的第三方库,所以你需要pip安装,安装过程就略过了.它基于urllib,采用 Apache2 Licensed 开源协议的 HTT ...

  2. 给requests模块添加请求头列表和代理ip列表

    Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,符合了Python语言的思想,通俗的说去繁存 ...

  3. requests模块基础

    requests模块 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bor ...

  4. python网络爬虫之二requests模块

    requests http请求库 requests是基于python内置的urllib3来编写的,它比urllib更加方便,特别是在添加headers, post请求,以及cookies的设置上,处理 ...

  5. 模块讲解---time模块,datetime模块,random模块,hashlib模块和hmac模块,typing模块,requests模块,re模块

    目录 1. 包 2. time模块   1. 优先掌握 2. 了解 3. datetime模块   1. 优先掌握 4. random模块   1. 优先掌握   2. 了解 5. hashlib模块 ...

  6. 爬虫(2) - Requests(1) | Requests模块的深度解析

    1.Requests 安装与请求方法 requests官方文档:https://docs.python-requests.org/zh_CN/latest/,官方文档不知道为什么挂了,访问不了.我找了 ...

  7. 爬虫requests模块 1

    让我们从一些简单的示例开始吧. 发送请求¶ 使用 Requests 发送网络请求非常简单. 一开始要导入 Requests 模块: >>> import requests 然后,尝试 ...

  8. requests 模块

    发送请求 使用Requests发送网络请求非常简单. 一开始要导入Requests模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取Gith ...

  9. requests模块--python发送http请求

    requests模块 在Python内置模块(urllib.urllib2.httplib)的基础上进行了高度的封装,从而使得Pythoner更好的进行http请求,使用Requests可以轻而易举的 ...

随机推荐

  1. hihocoder 1284 - 机会渺茫

    N有N_cnt个约数,M有M_cnt个约数,那么总共有N_cnt * M_cnt种对应情况. 假设其中有D_cnt个对应结果是相等的,而这D_cnt个数正好是gcd(N,M)的所有约数. 例如: N= ...

  2. 【紫书】Play on Words UVA - 10129 欧拉回路

    题意:给你1e5个字符串,若前一个的末尾字母等于当前的首字母,则可以连在一起(成语接龙一个意思)判断是否可以将他们连在一起 题解:将首位看作点,单词看作边.变成欧拉回路问题. 判断出入度是否相等,再用 ...

  3. Hadoop Single Node Setup(hadoop本地模式和伪分布式模式安装-官方文档翻译 2.7.3)

    Purpose(目标) This document describes how to set up and configure a single-node Hadoop installation so ...

  4. Oracle体系结构之Oracle基本数据字典:v$database、v$instance、v$version、dba_objects

    v$database: 视图结构: SQL> desc v$database; Name                                      Null?    Type - ...

  5. SpringMVC:JSON形式输出(基于Fastjson)

    在Spring3.0中,@ResponseBody标记可以将对象"封装"为JSON形式的数据,并输出,下面的例子中使用的是阿里的Fastjson JSONaz解析工具,在sprin ...

  6. 洛谷 P4697 Balloons [CEOI2011] 单调栈/dp (待补充qwq)

    正解:单调栈/dp 解题报告: 先放个传送门qwq 话说这题是放在了dp的题单里呢?但是听说好像用单调栈就可以做掉所以我就落实下单调栈的解法好了qwq (umm主要如果dp做好像是要斜率优化凸壳维护双 ...

  7. CentOS关闭防火墙&SELinux

    须知: 防火墙配置文件:/etc/sysconfig/iptables 查看防火墙状态:service iptables status 关闭防火墙:service iptables stop 关闭ip ...

  8. 004-spring cloud gateway-网关请求处理过程

    一.网关请求处理过程 客户端向Spring Cloud Gateway发出请求.如果网关处理程序映射确定请求与路由匹配,则将其发送到网关Web处理程序.此处理程序运行通过特定于请求的过滤器链发送请求. ...

  9. [py]py2自带Queue模块实现了3类队列

    py2自带Queue实现了3类队列 先搞清楚几个单词 Queue模块实现了三类队列: FIFO(First In First Out,先进先出,默认为该队列), 我们平时泛指的队列, LIFO(Las ...

  10. find the safest road(弗洛伊德)

    http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <iostream> #include <stdio.h> #i ...