requests(第三方模块) 请求、登录、下载网页
import requests #http://docs.python-requests.org/en/latest/api/ 说明文档
'''
requests.request(method,url,**kwargs)
requests.head(url,**kwargs)
requests.get(url,params=None,**kwargs)
requests.post(url,data=None,json=None,**kwargs)
requests.put(url,data=None,**kwargs)
requests.patch(url,data=None,**kwargs)
requests.delete(url,**kwargs)
上述7种方法返回的都是(Requests.)Response对象
method~请求对象的方法,猜测可选值是head,get,post,put,patch,delete
url~请求对象的URL
以下的可选参数都可通过**kwargs字典参数传入
params~ Dictionary or bytes to be sent in the query(疑问) string for the Request
data~ 需要传入request中的(字典类型,元组构成的列表类型[(key,value)](will be form encoded),二进制类型或file-like类型的)对象
json~ 需要被传入request的json类型数据
header~随request传入的字典类型的HTTP Headers 数据
files~ 字典类型的数据形如{'name':file-like-objects}或{'name':file-tuple}进行各种编码形式的上传。file-tuple可以是一个两、三、四元素的元组
('filename',fileobj,'content_type',custom_headers),'content-type'是str类型的描述文本的格式的字符串。
custom_headers是一个字典类型的参数,包含一些需要被添加到文件上的额外的标题信息
auth~认证元组 to enable Basic/Digest/Custom HTTP Auth
timeout~(入参为浮点类型或元组类型),浮点类型的话表示多少秒后放弃等待接收服务器发送的数据,2元素的元组格式为(connect timeout,read timeout)
allow_redirects~(布尔类型入参)是否允许 重定向,默认为True (GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD)redirection
proxies~ 字典类型的URL代理映射协议 Dictionary mapping proctocol to the URL of the proxy
verify~ 布尔型数据,表示我们是否核实了服务器的TLS证书,或者字符串型数据,表示可链接到CA bundle的路径。默认值是True
stream~ 如果设置为False,response content 就会立即被下载
cert~ 如果是字符串就就是通往ssl client 某个文件的路径(.pem) 元组类型的话形如('cert','key')
'''
#Requests Exceptions
'''
requests.RequestException(*args,**kwargs) 包含大量的错误类型There was an ambiguous exception that occurred while handling your request.
requests.ConnectionError(*args,**kwargs) 连接类型的错误 A Connection error occurred
requests.HTTPError(*args,**kwargs) HTTP错误 An HTTP error occurred
requests.URLRequired(*args,**kwargs) 无效的URL地址 A valid URL is required to make a request
requests.TooManyRedirects(*args,**kwargs) 重定向太多次了 TOO many redirects
requests.ConnectTimeout(*args,**kwargs) 连接超时 Requests that produced this error are safe to retry
requests.ReadTimeout(*args,**kwargs) 服务器没有返回任何数据,在给定的时间里
requests.Timeout(*args,**kwargs) 请求超时,捕获该错误类型,ConnectTimeout和ReadTimeout类型的错误都会被捕获。
'''
#requests.Session类
#Cookie :当访问某个站点时,随某个HTML网页发送到你的浏览器中的一小段信息;网络跟踪器(记录上网用户信息的软件)
#session提供了持续的cookie(记录,用户追踪?),连接池(不懂什么意思),和配置信息
import requests
s=requests.Session()
s.get('http://httpbin.org/get')
#也可写成
with requests.Session() as s :
s.get('http://httpbin.org/get')
'''
auth=None Default Authentication tuple or object to attach to Request.
cert=None SSL client certificate default, if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
close() Closes all adapters(适配器?) and as such the session
cookies=None 包含session里设置的所有未完成的cookie。装cookie的容器。By default it is a RequestsCookieJar, but may be any other cookielib.CookieJar compatible object.
下面的方法同requests的方法,返回的都是requests.Response对象
{delete(url,**kwargs)
get(url,**kwargs)
head(url,**kwargs)
options(url,**kwargs)
post(url,data=None,json=None,**kwargs)
put(url,data=None,**kwargs)
request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True,
proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None)比requests.request()多一个参数cookies
cookies ~ Dict or CookieJar Object to send with Request
}
send(request,**kwargs) 传递给一个准备好的request对象,返回类型requests.Response
stream=None Stream response content default.
trust_env=None 为代理配置信息标记的受信任设置,默认是认证的和熟悉的default authentication and similar
verify=None 默认SSL认证 SSL(Security Socket Layer 加密套接字协议层)
requests(第三方模块) 请求、登录、下载网页的更多相关文章
- Python-Windows下安装BeautifulSoup和requests第三方模块
http://blog.csdn.net/yannanxiu/article/details/50432498 首先给出官网地址: 1.Request官网 2.BeautifulSoup官网 我下载的 ...
- python接口测试中安装whl格式的requests第三方模块
下载 安装 requests第三方模块 下载:http://docs.python-requests.org/en/latest/user/install/#install 我下载是 https:// ...
- 用python的requests第三方模块抓取王者荣耀所有英雄的皮肤
本文使用python的第三方模块requests爬取王者荣耀所有英雄的图片,并将图片按每个英雄为一个目录存入文件夹中,方便用作桌面壁纸 下面时具体的代码,已通过python3.6测试,可以成功运行: ...
- python安装requests第三方模块
2018-08-28 22:04:51 1 .下载到桌面后解压,放到python的目录下 ------------------------------------------------------- ...
- requests模拟浏览器请求模块初识
requests模拟浏览器请求模块初识 一.下载 requests模拟浏览器请求模块属于第三方模块 源码下载地址http://docs.python-requests.org/zh_CN/lates ...
- python基础-7模块,第三方模块安装方法,使用方法。sys.path os sys time datetime hashlib pickle json requests xml
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- Python基础之模块:5、 第三方模块 requests模块 openpyxl模块
目录 一.第三方模块的下载与使用 1.什么是第三方模块 2.如何安装第三方模块 方式一:pip工具 方式二:pycharm中下载 3.注意事项 1.报错并有警告信息 2.报错,提示关键字 3.报错,无 ...
- 第四十节,requests模拟浏览器请求模块初识
requests模拟浏览器请求模块初识 requests模拟浏览器请求模块属于第三方模块 源码下载地址http://docs.python-requests.org/zh_CN/latest/use ...
- 第三百八十三节,Django+Xadmin打造上线标准的在线教育平台—第三方模块django-simple-captcha验证码
第三百八十三节,Django+Xadmin打造上线标准的在线教育平台—第三方模块django-simple-captcha验证码 下载地址:https://github.com/mbi/django- ...
- python-day3爬虫基础之下载网页
今天主要学习了关于网页下载器的一些内容,下边做一下总结: 1.网页下载器,顾名思义,就是将URL所对应的网页以HTML的形式下载到本地,最终存储成本地文件或者还是本地内存字符串,然后进行后续的分析与处 ...
随机推荐
- Python全栈开发-Day1-Python基础1
目录 Python介绍 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语句 表达式f ...
- RHEL 5 , 用安装CD作为YUM的Repository
官方文档写的非常好 14.5. Upgrading the System Off-line with ISO and Yum Create a target directory to mount yo ...
- Python自学:第二章 合并(拼接字符串)
first_name = "ada" last_name = "lovelace" full_name = first_name + " " ...
- 4.1.5 Georigia and Bob
Problem description: Georgia and Bob 在玩一个游戏. 如图所示,排成直线的格子上放有n个棋子.棋子 i 在左数第 Pi 个格子上.Georgia 和 Bob 轮流选 ...
- 第二类Stirling数
第二类斯特林数 第二类Stirling数:S2(p, k) 1.组合意义:第二类Stirling数计数的是把p个互异元素划分为k个非空集合的方法数 2.递推公式: S2(0, 0) = 1 S2(p, ...
- shiro中JSP标签
Shiro提供了JSTL标签用于在JSP/GSP页面进行权限控制,如根据登录用户显示相应的页面按钮. 导入标签库 <%@taglib prefix="shiro" uri=& ...
- Shiro中Realm
6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系 即用户-角色 ...
- LINQ 常用from
单个form子句string[] values = { "LINQ学习", "LINQ基本语句", "from子句", "单个fr ...
- 1002. Find Common Characters查找常用字符
参考:https://leetcode.com/problems/find-common-characters/discuss/247573/C%2B%2B-O(n)-or-O(1)-two-vect ...
- noip2014无线网络发射器选址
题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的 129 条东西向街道和 129 条南北向街道所形成的网格状,并且 ...