笔记-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

  1. 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

  1. 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.

常用方法/属性:

  1. auth = None

Default Authentication tuple or object to attach to Request.

  1. cert = None

SSL client certificate default, if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.

  1. close()[source]

Closes all adapters and as such the session

  1. 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.

  1. delete(url, **kwargs)[source]

Sends a DELETE request. Returns Response object.

  1. get(url, **kwargs)[source]

Sends a GET request. Returns Response object.

  1. head(url, **kwargs)[source]

Sends a HEAD request. Returns Response object.

  1. headers = None

A case-insensitive dictionary of headers to be sent on each Request sent from this Session.

  1. post(url, data=None, json=None, **kwargs)[source]

Sends a POST request. Returns Response object.

  1. 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.

  1. 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请求返回内容的类。查看返回信息。

  1. apparent_encoding

The apparent encoding, provided by the chardet library.

  1. 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.

  1. content:Content of the response, in bytes.
  2. cookies = None

A CookieJar of Cookies the server sent back.

  1. 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.

  1. encoding = None

Encoding to decode with when accessing r.text.

  1. headers = None

Case-insensitive Dictionary of Response Headers. For example, headers['content-encoding'] will return the value of a 'Content-Encoding'response header.

  1. 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.

  1. is_permanent_redirect

True if this Response one of the permanent versions of redirect.

  1. is_redirect

True if this Response is a well-formed HTTP redirect that could have been processed automatically (by Session.resolve_redirects).

  1. links:Returns the parsed header links of the response, if any.
  2. next:Returns a PreparedRequest for the next request in a redirect chain, if there is one.
  3. 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.

  1. raise_for_status()[source]:Raises stored HTTPError, if one occurred.
  2. reason = None

Textual reason of responded HTTP Status, e.g. “Not Found” or “OK”.

  1. request = None

The PreparedRequest object to which this is a response.

  1. status_code = None

Integer Code of responded HTTP Status, e.g. 404 or 200.

  1. 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.

  1. 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信息时使用。

  1. 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.

  1. 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常用类/方法/属性的更多相关文章

  1. Java && Python 算法面试常用类以及方法总结

    数据结构 逻辑结构上: 包括集合,线性结构,非线性结构. 存储结构: 顺序存储,链式存储,索引存储,散列存储. Java 常见数据结构 大专栏  Java && Python 算法面试 ...

  2. JavaSE学习笔记(8)---常用类

    JavaSE学习笔记(8)---常用类 1.Object类 java.lang.Object类是Java语言中的根类,即所有类的父类.它中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类 ...

  3. python中requests库使用方法详解

    目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...

  4. python进阶之类常用魔法方法和魔法属性

    前言 前面我们总结过了python的关键字.运算符.内置函数.语法糖等与python魔法方法之间的关系,现在我们更细一点,看看python的面向对象编程有哪些常用的魔法属性和魔法方法. 魔法属性 对于 ...

  5. Python基础:新式类的属性访问

    一.概述 二.准备工作 1.讨论对象 2.名词解释 三.实例绑定的属性访问 1.获取属性 一般规则 参考源码 示例验证 2.设置属性 一般规则 参考源码 示例验证 3.删除属性 一般规则 参考源码 示 ...

  6. python面向对象-2深入类的属性

    在交互式环境中输入: >>> class A: a=0 def __init__(self): self.a=10 self.b=100 >>> a=A() > ...

  7. python之面向对象的成员,方法,属性,异常处理

    一.类的私有成员 1. 类中的私有成员是什么? 私有:只有满足一部分条件的才能使用 私有类的属性 私有对象的属性 私有方法 正常状态 class B: school_name = '老男孩教育' de ...

  8. jQuery中一些不常用的方法属性【转载】

    index(subject) 搜索与参数表示的对象匹配的元素,并返回相应元素的索引值.如果找到了匹配的元素,从0开始返回:如果没有找到匹配的元素,返回-1. data() data(elem):为页面 ...

  9. PHP判断{函数/类/方法/属性}是否存在

    1.php判断系统函数或自己写的函数是否存在 bool function_exists ( string $function_name ) 判断函数是否已经定义,例如: if(function_exi ...

随机推荐

  1. If you want the rainbow, you have to deal with the rain.

    If you want the rainbow, you have to deal with the rain.想要彩虹,就先忍受雨水.

  2. spring-cloud构架微服务(2)-全局配置二

    接上篇,实际项目中,可能会遇到有些配置项,例如:邮件地址.手机号等在服务已经上线之后做了改动(就当会出现这种情况好了).然后你修改了配置信息,就得一个一个去重启对应的服务.spring-全局配置提供了 ...

  3. Android 修改TabLayout底部导航条Indicator的长短

    关于Tablayout,使用的应该很频繁了,但是底部导航条长短是固定死的,需要自己来改动长短,找了半天没找着方法,看了下官方建议,可以通过映射来修改自己想要的长短,其实也就几行代码的问题,看代码: p ...

  4. Thymeleaf基础知识

    Thymeleaf是一个Java类库,它是一个xml/xhtml/html5的模板引擎,可以作为MVC的Web引用的View层. Thymeleaf还提供了额外的模块与SpringMVC集成,因此推荐 ...

  5. 再谈 Struts1.x 的运行机制

    1.Action类 execute 方法 ActionMapping 对应 <action path="user" type="myuser.UserAction& ...

  6. 怎样下载YouTube 4K视频

    随着科技的进步,人们生活水平的提高,视频的清晰度也越来越高,以前那个观看模糊视频的时代已经一去不复返了.从最开始的720P和1080P高清视频,再到2K,进而到如今的4K(即3840×2160)极清视 ...

  7. 前端高质量知识(四)-JS详细图解作用域链与闭包

    攻克闭包难题 初学JavaScript的时候,我在学习闭包上,走了很多弯路.而这次重新回过头来对基础知识进行梳理,要讲清楚闭包,也是一个非常大的挑战. 闭包有多重要?如果你是初入前端的朋友,我没有办法 ...

  8. Mysql在字符串类型的日期上加上10分钟并和如今的日期做比較

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/ufo2910628/article/details/32092869 SELECT id FROM ...

  9. 广搜最短路(最短时间到达目的地),POJ(3669)

    题目链接:http://poj.org/problem?id=3669 解题报告: 1.流星坠落的点,四周和自己本身都被毁灭,不断更新每个点被毁灭的时候的最短时间. 2.搜索终点是,到达某个点,这个不 ...

  10. 广搜最短路径变形,(POJ3414)

    题目链接:http://poj.org/problem?id=3414 解题报告: 1.每个节点都是一个独立的状态 2.这里的状态转移就是有几种出路,4种:1.倒掉a中的水,2.把a中的水倒到b中去, ...