Python requests介绍之接口介绍

Python requests介绍

引用官网介绍

Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用。

Requests 允许你发送纯天然,植物饲养的 HTTP/1.1 请求,无需手工劳动。你不需要手动为 URL 添加查询字串,也不需要对 POST 数据进行表单编码。Keep-alive 和 HTTP 连接池的功能是 100% 自动化的,一切动力都来自于根植在 Requests 内部的 urllib3。

功能特性

Requests 完全满足今日 web 的需求。

  • Keep-Alive & 连接池
  • 国际化域名和 URL
  • 带持久 Cookie 的会话
  • 浏览器式的 SSL 认证
  • 自动内容解码
  • 基本/摘要式的身份认证
  • 优雅的 key/value Cookie
  • 自动解压
  • Unicode 响应体
  • HTTP(S) 代理支持
  • 文件分块上传
  • 流下载
  • 连接超时
  • 分块请求
  • 支持 .netrc

Requests 支持 Python 2.6—2.7以及3.3—3.7,而且能在 PyPy 下完美运行。

''' requests 接口介绍  '''

#-*-coding:utf-8-*-
# Time:2017/10/12 22:54
# Author:YangYangJun#开始 requests学习之旅,这里直接在代码编写文字了。#requests 是python的第三方库,所以这里需要安装# 直接在命令行窗口执行 pip install requests  即可#如果提示安装失败,可以参看提示,安装必备软件然后重新安装即可#安装成功后 即可直接导入import  requests#如上不报错即可#参考资料#访问网址 http://cn.python-requests.org/zh_CN/latest/  即可查看官方中文文档#或是查看源代码  https://github.com/requests/requests#也可到 自己的安装目录下查找 D:/SProgram/Python/Lib/site-packages/requests/api.py 等文件查看介绍''' requests 接口介绍  '''
# 这部分文档包含了 Requests 所有的接口。对于 Requests 依赖的外部库部分,我们在这里介绍最重要的部分,并提供了规范文档的链接。
# 
# 主要接口
# Requests 所有的功能都可以通过以下 7 个方法访问。它们全部都会返回一个 Response 对象的实例。# 1、request()方法
# requests.request(method, url, **kwargs)   Constructs and sends a Request. 构造和发送请求。
# 这里的参数 method 、 url 是必选参数,**kwargs 是可选参数,是 可变关键字参数,Dictionary 类型
'''
参数:    
method -- method for the new Request object.  
新请求对象的方法
url -- URL for the new Request object.  
新的请求对象的URL
params -- (optional) Dictionary or bytes to be sent in the query string for the Request.  
(可选)在查询字符串中为请求发送的字典或字节。
data -- (optional) Dictionary or list of tuples [(key, value)] (will be form-encoded), bytes, or file-like object to send in the body of the Request.
(可选)字典或元组列表[(键,值)](将以表单编码)、字节或类似文件的对象发送到请求体中。
json -- (optional) json data to send in the body of the Request.
(可选)json数据发送到请求体中。
headers -- (optional) Dictionary of HTTP Headers to send with the Request.
(可选)HTTP报头字典,以发送请求。
cookies -- (optional) Dict or CookieJar object to send with the Request.
(可选)命令或CookieJar对象发送请求。
files -- (optional) Dictionary of 'name': file-like-objects (or {'name': file-tuple}) for multipart encoding upload. file-tuple can be a 2-tuple 
('filename', fileobj), 3-tuple ('filename', fileobj, 'content_type') or a 4-tuple ('filename', fileobj, 'content_type', custom_headers), 
where 'content-type' is a string defining the content type of the given file and custom_headers a dict-like object containing additional 
headers to add for the file.
438/5000  
(可选)“名称”的字典:文件- like- object(或{' name ':file - tuple}),用于多部分编码上传。文件- tuple可以是一个2元组(文件名,fileobj),
3元组(文件名,fileobj,' content_type ')或一个4元组(' filename ',fileobj,' content_type ',custom_headers),
其中' content-type '是一个字符串,它定义了给定文件的内容类型和custom_headers,其中包含了一个类似于dict的对象,其中包含了添加文件的附加头文件。auth -- (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
(可选)Auth tuple来启用Basic /Digest /自定义HTTP Auth。
timeout (float or tuple) -- (optional) How many seconds to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.
(可选)在放弃之前,等待服务器发送数据的时间是多少秒,作为一个浮点数,或者是一个(连接超时,读超时)元组。
allow_redirects (bool) -- (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to True.
(可选)布尔。启用/禁用GET /选项/ POST / PUT /补丁/删除/头重定向。默认值为True。
proxies -- (optional) Dictionary mapping protocol to the URL of the proxy.
(可选)字典映射协议到代理的URL。
verify -- (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to True.
(可选)一个布尔值,在这种情况下,它控制我们是否验证服务器的TLS证书,或者是一个字符串,在这种情况下,它必须是一个到CA包使用的路径。默认值为True。
stream -- (optional) if False, the response content will be immediately downloaded.
(可选)如果错误,将立即下载响应内容。
cert -- (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
(可选)如果字符串,路径到ssl客户机cert文件(. pem)。如果是Tuple(' cert ',' key ')对。
'''# request()方法实例
req = requests.request('GET', 'http://httpbin.org/get')print req
# 得到 <Response [200]># 2、head()方法# requests.head(url, **kwargs)
# Sends a HEAD request.  发送一个HEAD 请求。# Optional arguments that ``request`` takes.  请求获取的可选参数。
r"""Sends a HEAD request.:param url: URL for the new :class:`Request` object.:param \*\*kwargs: Optional arguments that ``request`` takes.  :return: :class:`Response <Response>` object  :rtype: requests.Response"""
# head()方法实例
req = requests.head('http://httpbin.org/get')print req
# 得到 <Response [200]># 3、get()方法# requests.get(url, params=None, **kwargs)[源代码]
# Sends a GET request.
#
# 参数:
# url -- URL for the new Request object.
# params -- (optional) Dictionary or bytes to be sent in the query string for the Request.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response# get()方法实例
req = requests.get('http://httpbin.org/get')print req
# 得到 <Response [200]># 4、post()方法# requests.post(url, data=None, json=None, **kwargs)[源代码]
# Sends a POST request.
#
# 参数:
# url -- URL for the new Request object.
# data -- (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request.
# json -- (optional) json data to send in the body of the Request.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response# post()方法实例
req = requests.post('http://httpbin.org/get')print req
# 得到 <Response [405]># 5、put()方法# requests.put(url, data=None, **kwargs)[源代码]
# Sends a PUT request.
#
# 参数:
# url -- URL for the new Request object.
# data -- (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request.
# json -- (optional) json data to send in the body of the Request.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response# 6、patch()方法# requests.patch(url, data=None, **kwargs)[源代码]
# Sends a PATCH request.
#
# 参数:
# url -- URL for the new Request object.
# data -- (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request.
# json -- (optional) json data to send in the body of the Request.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response# 7、delete()方法# requests.delete(url, **kwargs)[源代码]
# Sends a DELETE request.
#
# 参数:
# url -- URL for the new Request object.
# **kwargs -- Optional arguments that request takes.
# 返回:
# Response object
#
# 返回类型:
# requests.Response
#

 

转载于:https://www.cnblogs.com/BlueSkyyj/p/7652860.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415590.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习(2623):Vuex初步识别

Vuex 是什么&#xff1f; Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态&#xff0c;并以相应的规则保证状态以一种可预测的方式发生变化。Vuex 也集成到 Vue 的官方调试工具 devtools extension (opens new window)&#x…

3 微信公众号开发 接受普通消息

用户如果在公众号发送内容&#xff0c;微信服务器会把消息转发到我们到服务器上&#xff0c;我们需要及时做出处理&#xff0c;给用户反馈。 文档地址&#xff1a;https://mp.weixin.qq.com/wiki?tresource/res_main&idmp1421140453 例如&#xff0c;在微信公众号发送消…

Android 动画以view中心点旋转动画

旋转180度 Animation anim new RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); anim.setFillAfter(true); // 设置保持动画最后的状态 anim.setDuration(500); // 设置动画时间 anim.setInterpolator(new AccelerateInterpo…

Linux 查看系统硬件信息

Linux 查看系统硬件信息(实例详解)linux查看系统的硬件信息&#xff0c;并不像windows那么直观&#xff0c;这里我罗列了查看系统信息的实用命令&#xff0c;并做了分类&#xff0c;实例解说。 cpu lscpu命令&#xff0c;查看的是cpu的统计信息. blueblue-pc:~$ lscpu Archit…

简易表格编辑器

SMMS有个建表工具,尝试使用HTML模仿出一个简单的做为练习. 截图: 一.实现SMMS建表工具的操作 点击单元格,进入编辑状态.按TAB切换单元格按方向箭切换单元格按空格设定取消主键列二.实现思路: 1.DOM结构使用div(行)span(列) <div><span></span><span>&l…

前端学习(2624):state

State # 单一状态树 在 Scrimba 上尝试这节课 Vuex 使用单一状态树——是的&#xff0c;用一个对象就包含了全部的应用层级状态。至此它便作为一个“唯一数据源 (SSOT (opens new window))”而存在。这也意味着&#xff0c;每个应用将仅仅包含一个 store 实例。单一状态树让…

Android 向右滑返回,退出当前activity

demo链接&#xff1a;https://download.csdn.net/download/meixi_android/10826597 实现效果: 纯原生类实现方法&#xff1a; 1、自定义侧滑基类SwipeBackActivity public class SwipeBackActivity extends FragmentActivity implements SwipeBackActivityBase {private Swi…

4 微信公众号开发 被动回复消息 回复没有反应怎么办

接收到用户的消息&#xff0c;我们需要做出反馈。 文档地址&#xff1a;https://mp.weixin.qq.com/wiki?tresource/res_main&idmp1421140543 官方返回的数据格式是xml&#xff0c;我们需要转化为json。获取数据&#xff0c;构造xml在返回就OK了。 核心代码&#xff1a; …

Linux系统的远程登录

Linux大多应用于服务器&#xff0c;而服务器不可能像PC一样放在办公室&#xff0c;它们是放在IDC机房的&#xff0c;所以我平时登录linux系统都是通过远程登录的。Linux系统中是通过ssh服务实现的远程登录功能。默认ssh服务开启了22端口&#xff0c;而且当我们安装完系统时&…

更新——Canvas画布动画效果之实现倒计时

Hello&#xff0c;大家好&#xff01; 小W复活啦&#xff01;继续欢乐的给大家更博&#xff0c;输送新知识~~ 不开玩笑啦&#xff01;秒进正题~~~ 上次更博&#xff0c;小W给大家介绍了Canvas画布的基础部分&#xff0c;以及实现了一个由7*10点阵图显示的倒计时的基本架构。 上…

5 微信公众号开发 获取 access_token

在使用微信公众号接口中&#xff0c;需要access_token。access_token是公众号的全局唯一凭证。可以理解为我们服务器的身份证。 总结以上说明&#xff0c;access_token需要做到以下两点&#xff1a; 1.因为access_token有2个小时的时效性&#xff0c;要有一个机制保证最长2个…

Linux平台上最常用的翻译工具—StarDict(星际译王)。

1、打开终端&#xff0c;输入命令$sudo apt-get install stardict&#xff0c;开始下载安装。 2、安装完成后&#xff0c;打开后选择左下角取词选项&#xff0c;即可完成屏幕取词功能。 3、以上是实现的是在线翻译功能&#xff0c;要实现离线翻译&#xff0c;需要下载离线辞…

Android TextView 设置文字背景色或文字颜色,字体阴影,字体样式

//第一个字符变色String copiesStr_notic tv_notic.getText().toString().trim();SpannableString spannableString2 new SpannableString(copiesStr_notic);//0 第一行缩进像素 , SizeUtils.dp2px(15)非第一行缩进像素Paint mPaintnew Paint();float wmPaint.measureText(cop…

resharper license server

2018-5-14更新 http://jetbrains-a.pw good 2018-4-17 更新 http://jetbrains.tools bad 144.202.4.96 good 2017-10-25 更新 http://idea.iteblog.com/key.php bad http://idea.lanyus.com/ bad http://jetbrains.tech bad http://www.iteblog.com/idea/key.php bad http://xi…

ubuntu man手册完善

ubuntu man手册完善 Linux提供了丰富的帮助手册&#xff0c;当你需要查看某个命令的参数时不必到处上网查找&#xff0c;只要man一下即可。 sudo apt-get install manpages-dev sudo apt-get install manpages-posix sudo apt-get install manpages-posix-dev sudo apt-get ins…

微信公众号 和 微信小程序 用户数据互通 通过微信开放平台的UnionID机制

很多时候&#xff0c;需要将多个公众号和小程序的用户数据打通&#xff0c;我们需要做的&#xff0c;就是将这些公众号和小程序都绑定到同一个微信开发平台上&#xff0c;那么我们就可以获取到UnionID了。其实数据就已经打通了。 UnionID机制说明&#xff1a; 如果开发者拥有…

Android TextView长按复制实现,Android复制文本,Selectable

代码调用系统复制粘贴板 //获取剪贴板管理器&#xff1a;ClipboardManager cm (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); // 创建普通字符型ClipDataClipData mClipData ClipData.newPlainText("Label", "这里是要复制的文字");…

find命令 -- 之查找指定时间内修改过的文件

比如我们要查找linux下指定时间内做过改动的文件&#xff0c;我们可以用find命令&#xff0c;其实find命令的功能十分强大&#xff0c;下面我们通过几个简单的例子来学习下find命令的简单用法&#xff1a;find /opt -iname "*" -atime 1 -type f找出 /opt 下一天前访…

Fiddler 学习笔记

Fiddler是啥&#xff1f; 百度百科里是这样介绍它的 - “Fiddler是一个web调试代理。它能够记录所有客户端和服务器间的http请求&#xff0c;允许你监视&#xff0c;设置断点&#xff0c;甚至修改输入输出数据&#xff0c;fiddler包含了一个强大的基于事件脚本的子系统&#xf…