没有合适的资源?快使用搜索试试~ 我知道了~
python_requests快速入门.pdf
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 105 浏览量
2021-11-15
08:57:11
上传
评论
收藏 254KB PDF 举报
温馨提示
![preview](https://dl-preview.csdnimg.cn/42205981/0001-9ea63304623c5383b90a643168ab1e06_thumbnail.jpeg)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
试读
25页
python_requests快速入门.pdf
资源推荐
资源详情
资源评论
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/release/download_crawler_static/42205981/bg1.jpg)
Python requests 快速入门
迫不及待了吗?本页内容为如何入门
Requests
提供了很好的指引。其假设你已经安装了
Requests。如果还没有, 去 安装 一节看看吧。
首先,确认一下:
Requests 已安装
Requests是
最新的
让我们从一些简单的示例开始吧。
发送请求
使用 Requests发送网络请求非常简单。
一开始要导入 Requests模块 :
>>> import requests
然后,尝试获取某个网页。本例子中,我们来获取 Github 的公共时间线
>>> r = requests.get('https://github.com/timeline.json' )
现在,我们有一个名为
r
的
Response
对象。可以从这个对象中获取所有我们想要的信息。
Requests简便的 API 意味着所有 HTTP 请求类型都是显而易见的。 例如,你可以这样发送一
个 HTTP POST 请求 :
>>> r = requests.post("http://httpbin.org/post" )
漂亮,对吧?那么其他 HTTP 请求类型: PUT , DELETE , HEAD 以及 OPTIONS 又是如
何的呢?都是一样的简单 :
>>> r = requests.put( "http://httpbin.org/put" )
>>> r = requests.delete("http://httpbin.org/delete" )
>>> r = requests.head("http://httpbin.org/get" )
![](https://csdnimg.cn/release/download_crawler_static/42205981/bg2.jpg)
>>> r = requests.options( "http://httpbin.org/get" )
都很不错吧,但这也仅是 Requests的冰山一角呢。
为 URL 传递参数
你也许经常想为 URL 的查询字符串 (query string) 传递某种数据。如果你是手工构建 URL ,
那么数据会以键 /值 对的形式置于 URL 中,跟在一个问号的后面。例如, httpbin.org/get?
key=val 。 Requests允许你使用 params关键字参数,以一个字典来提供这些参数。 举例来说,
如果你想传递
key1=value1
和
key2=value2
到
httpbin.org/get
,那么你可以使用如下代码
:
>>> payload = { 'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get" , params=payload)
通过打印输出该 URL ,你能看到 URL 已被正确编码 :
>>> print r.url
u'http://httpbin.org/get?key2=value2&key1=value1'
响应内容
我们能读取服务器响应的内容。再次以 Github 时间线为例 :
>>> import requests
>>> r = requests.get('https://github.com/timeline.json' )
>>> r.text
'[{"repository":{"open_issues":0,"url":"https://github.com/...
Requests会自动解码来自服务器的内容。大多数 unicode 字符集都能被无缝地解码。
请求发出后, Requests会基于 HTTP 头部对响应的编码作出有根据的推测。当你访问 r.text
之时, Requests会使用其推测的文本编码。 你可以找出 Requests使用了什么编码,并且能够
使用
r.encoding
属性来改变它
:
![](https://csdnimg.cn/release/download_crawler_static/42205981/bg3.jpg)
>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1'
如果你改变了编码,每当你访问 r.text ,Request都将会使用 r.encoding 的新值。
在你需要的情况下, Requests也可以使用定制的编码。如果你创建了自己的编码,并使用
codecs 模块进行注册,你就可以轻松地使用这个解码器名称作为 r.encoding 的值, 然后由
Requests
来为你处理编码。
二进制响应内容
你也能以字节的方式访问请求响应体,对于非文本请求 :
>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
Requests
会自动为你解码
gzip
和
deflate
传输编码的响应数据。
例如,以请求返回的二进制数据创建一张图片,你可以使用如下代码 :
>>> from PIL import Image
>>> from StringIO import StringIO
>>> i = Image.open(StringIO(r .content))
JSON 响应内容
Requests中也有一个内置的 JSON 解码器,助你处理 JSON 数据 :
>>> import requests
>>> r = requests.get('https://github.com/timeline.json' )
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
![](https://csdnimg.cn/release/download_crawler_static/42205981/bg4.jpg)
如果
JSON
解码失败,
r.json
就会抛出一个异常。
原始响应内容
在罕见的情况下你可能想获取来自服务器的原始套接字响应,那么你可以访问 r.raw 。如果
你确实想这么 干,那请你确 保在初 始请求中设置了
stream=True
。具体的你可以这么 做
:
>>> r = requests.get('https://github.com/timeline.json' , stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
定制请求头
如果你想为请求 添加 HTTP 头部, 只 要简单地传递一个 dict 给 headers 参数就可以了。
例如,在 前一个示例中我们没有指定 content-type:
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = { 'some': 'data'}
>>> headers= { 'content-type': 'application/json' }
>>> r = requests.post(url, data=json.dumps(payload), headers=headers)
更加复杂 的 POST 请求
通常,你想要发送一些编码为 表单形式的数据 —非常 像一个 HTML 表单。 要实现这个, 只
需简单地传递一个字典 给 data 参数。你的数据字典 在发出请求时会自动编码为 表单形式 :
![](https://csdnimg.cn/release/download_crawler_static/42205981/bg5.jpg)
>>> payload = { 'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post" , data=payload)
>>> print r.text
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}
很多时 候你想要发送的数据并非编码为 表单形式的。如果你传递一个
string
而不是一个
dict
,那么数据会被 直 接发 布出去。
例如, Github API v3 接受编码为 JSON 的 POST/PATCH 数据 :
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = { 'some': 'data'}
>>> r = requests.post(url, data=json.dumps(payload))
POST 一个多部 分编码 (Multipart-Encoded) 的文 件
Requests使得上 传多部 分编码文 件变得很简单 :
>>> url = 'http://httpbin.org/post'
>>> files = { 'file' : open('report.xls' , 'rb')}
剩余24页未读,继续阅读
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
honghong1hao
- 粉丝: 0
- 资源: 4万+
![benefits](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-1.c8e153b4.png)
下载权益
![privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-2.ec46750a.png)
C知道特权
![article](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-3.fc5e5fb6.png)
VIP文章
![course-privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-4.320a6894.png)
课程特权
![rights](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-icon.fe0226a8.png)
开通VIP
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)