笔记-python-lib-requests常用类/方法/属性
笔记-python-lib-requests常用类/方法/属性
1. requests模块常用类/方法/属性
在使用中发现对requests模块不够熟悉,写了几个案例后重新整理了一下文档,罗列出常使用的类/方法/属性。
详细情况请查看:http://docs.python-requests.org/en/master/
1.1. class requests.request()
requests.request(method, url, **kwargs)
Constructs and sends a Request.
Returns: Response object
Return type: requests.Response
- requests.get(url, params=None, **kwargs)
Sends a GET request.
Parameters:
url – URL for the new Request object.
params – (optional) Dictionary, list of tuples or bytes to send in the body of the Request.
**kwargs – Optional arguments that request takes.
Returns: Response object
Return type:requests.Response
- requests.post(url, data=None, json=None, **kwargs)[source]
Sends a POST request.
1.2. Request Sessions
最常用的功能类。
class requests.Session
A Requests session.Provides cookie persistence, connection-pooling, and configuration.
常用方法/属性:
- 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()[source]
Closes all adapters and as such the session
- cookies = None
A CookieJar containing all currently outstanding cookies set on this session. By default it is a RequestsCookieJar, but may be any other cookielib.CookieJarcompatible object.
- delete(url, **kwargs)[source]
Sends a DELETE request. Returns Response object.
- get(url, **kwargs)[source]
Sends a GET request. Returns Response object.
- head(url, **kwargs)[source]
Sends a HEAD request. Returns Response object.
- headers = None
A case-insensitive dictionary of headers to be sent on each Request sent from this Session.
- post(url, data=None, json=None, **kwargs)[source]
Sends a POST request. Returns Response object.
- prepare_request(request)[source]
Constructs a PreparedRequest for transmission and returns it. The PreparedRequest has settings merged from the Request instance and those of theSession.
- proxies = None
Dictionary mapping protocol or protocol and host to the URL of the proxy (e.g. {‘http’: ‘foo.bar:3128’, ‘http://host.name’: ‘foo.bar:4012’}) to be used on each Request.
1.3. class requests.Response
class requests.Response
The Response object, which contains a server’s response to an HTTP request.
表示HTTP请求返回内容的类。查看返回信息。
- apparent_encoding
The apparent encoding, provided by the chardet library.
- close()[source]
Releases the connection back to the pool. Once this method has been called the underlying raw object must not be accessed again.
Note: Should not normally need to be called explicitly.
- content:Content of the response, in bytes.
- cookies = None
A CookieJar of Cookies the server sent back.
- elapsed = None
The amount of time elapsed between sending the request and the arrival of the response (as a timedelta). This property specifically measures the time taken between sending the first byte of the request and finishing parsing the headers. It is therefore unaffected by consuming the response content or the value of the stream keyword argument.
- encoding = None
Encoding to decode with when accessing r.text.
- headers = None
Case-insensitive Dictionary of Response Headers. For example, headers['content-encoding'] will return the value of a 'Content-Encoding'response header.
- history = None
A list of Response objects from the history of the Request. Any redirect responses will end up here. The list is sorted from the oldest to the most recent request.
- is_permanent_redirect
True if this Response one of the permanent versions of redirect.
- is_redirect
True if this Response is a well-formed HTTP redirect that could have been processed automatically (by Session.resolve_redirects).
- links:Returns the parsed header links of the response, if any.
- next:Returns a PreparedRequest for the next request in a redirect chain, if there is one.
- ok:Returns True if status_code is less than 400, False if not.
This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code is between 200 and 400, this will return True. This is not a check to see if the response code is 200 OK.
- raise_for_status()[source]:Raises stored HTTPError, if one occurred.
- reason = None
Textual reason of responded HTTP Status, e.g. “Not Found” or “OK”.
- request = None
The PreparedRequest object to which this is a response.
- status_code = None
Integer Code of responded HTTP Status, e.g. 404 or 200.
- text
Content of the response, in unicode.
If Response.encoding is None, encoding will be guessed using chardet.
The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set r.encoding appropriately before accessing this property.
- url = None
Final URL location of Response.
1.4. RequestCookieJar
class requests.cookies.RequestsCookieJar(policy=None)
Compatibility class; is a cookielib.CookieJar, but exposes a dict interface.
需要查看cookie信息时使用。
- get_dict(domain=None, path=None)
Takes as an argument an optional domain and path and returns a plain old Python dict of name-value pairs of cookies that meet the requirements.
- items()
Dict-like items() that returns a list of name-value tuples from the jar. Allows client-code to call dict(RequestsCookieJar) and get a vanilla python dict of key value pairs.
笔记-python-lib-requests常用类/方法/属性的更多相关文章
- Java && Python 算法面试常用类以及方法总结
数据结构 逻辑结构上: 包括集合,线性结构,非线性结构. 存储结构: 顺序存储,链式存储,索引存储,散列存储. Java 常见数据结构 大专栏 Java && Python 算法面试 ...
- JavaSE学习笔记(8)---常用类
JavaSE学习笔记(8)---常用类 1.Object类 java.lang.Object类是Java语言中的根类,即所有类的父类.它中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类 ...
- python中requests库使用方法详解
目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...
- python进阶之类常用魔法方法和魔法属性
前言 前面我们总结过了python的关键字.运算符.内置函数.语法糖等与python魔法方法之间的关系,现在我们更细一点,看看python的面向对象编程有哪些常用的魔法属性和魔法方法. 魔法属性 对于 ...
- Python基础:新式类的属性访问
一.概述 二.准备工作 1.讨论对象 2.名词解释 三.实例绑定的属性访问 1.获取属性 一般规则 参考源码 示例验证 2.设置属性 一般规则 参考源码 示例验证 3.删除属性 一般规则 参考源码 示 ...
- python面向对象-2深入类的属性
在交互式环境中输入: >>> class A: a=0 def __init__(self): self.a=10 self.b=100 >>> a=A() > ...
- python之面向对象的成员,方法,属性,异常处理
一.类的私有成员 1. 类中的私有成员是什么? 私有:只有满足一部分条件的才能使用 私有类的属性 私有对象的属性 私有方法 正常状态 class B: school_name = '老男孩教育' de ...
- jQuery中一些不常用的方法属性【转载】
index(subject) 搜索与参数表示的对象匹配的元素,并返回相应元素的索引值.如果找到了匹配的元素,从0开始返回:如果没有找到匹配的元素,返回-1. data() data(elem):为页面 ...
- PHP判断{函数/类/方法/属性}是否存在
1.php判断系统函数或自己写的函数是否存在 bool function_exists ( string $function_name ) 判断函数是否已经定义,例如: if(function_exi ...
随机推荐
- [持续更新] Linux基础的重要命令
命令总结:100个左右 mkdir 方法一 [root@localhost ~]# mkdir /test && ls -ld /test 方法二 [root@localhost ~] ...
- Linux pptpd 的 iptables 包过滤设置
用Centos架设了一台pptpd vpn服务器,信息如下: 服务器IP 192.168.100.1 /24 网关 192.168.100.254(NAT防火墙,将 <外网IP>:1723 ...
- 快速排序c语言实现
#include <stdio.h> void quick_sort(int* a, int n) { ) return; int i,j,tmp,k; k = a[n/]; ,j = n ...
- C#变量、常量
变量 一.命名变量:标识符 为变量命名时要遵循C#语言的命名规范: 1.变量名只能由字母.数字和下划线组成,而不能包含空格.标点符号.运算符等其他符号 2.变量名不能与C#中的关键字名称相同 二. ...
- Struts2_访问Web元素
取得Map 类型的 request,session,application, HttpServletRequest,HttpSession,ServletContext的引用. 分访问 Map 类型和 ...
- Azure进阶攻略 | 下载还是在浏览器直接打开,MIME说了算!
多年来,从一开始的网络菜鸟发展成 Azure 云专家,想必你一定学到了很多知识.不知道在这个过程中你自己是否遇到过,或者被人问到过类似下面这样的问题: 同样是直接点击网页上提供的 .mp4 视频文件链 ...
- Azure资源模板化部署,伦家不懒都不好意思了
如果老板让你在云平台上部署一套系统,你准备怎么做? 嗯,估计得根据具体需求开通或创建一大堆东西:虚拟机.存储.数据库.虚拟网络……别急还没完,接着还要对这些东西的规模.配置等各方面调整和优化.一系列环 ...
- 第四章 T-SQL编程
1.前言->此T-SQL编程是基于sql server开发环境->关键字:T-SQL编程:游标:视图和索引 2.T-SQL编程基础->标识符:常规标识符必须以汉字.字母.下划线_.@ ...
- vos2009如何监听客户行业是否正规
在对接被叫改写规则中将改写规则由原来0:9150 改为0:9150+自己手机号,这样客户打出话全转接到自己手机上,可以接听客户销售行业:
- nginx处理HTTP header问题
在实际开发中遇到http header 自定义key中包含下划线(_)时服务端header丢失的问题,解决办法详细见以下网页内容,感谢原博主 http://blog.csdn.net/dac55300 ...