pythonrequests库详细说明

上传人:第*** 文档编号:61714719 上传时间:2018-12-10 格式:PDF 页数:15 大小:326.51KB
返回 下载 相关 举报
pythonrequests库详细说明_第1页
第1页 / 共15页
pythonrequests库详细说明_第2页
第2页 / 共15页
pythonrequests库详细说明_第3页
第3页 / 共15页
pythonrequests库详细说明_第4页
第4页 / 共15页
pythonrequests库详细说明_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《pythonrequests库详细说明》由会员分享,可在线阅读,更多相关《pythonrequests库详细说明(15页珍藏版)》请在金锄头文库上搜索。

1、open in browser PRO version Are you a developer? Try out the HTML to PDF API 这是 Google 对 http:/ 的缓存。 这是该网页在 2015年7月17日 10:22:22 GMT 的快照。 当前页在此期间可能已经更改。 了解详情 完整版本纯文字版本查看源代码提示:要在此页面上快速找到您的搜索字词,请按 Ctrl+F 或者 ?-F(苹果机),然后使用查找栏搜索。 首 页WEB前端编程语言Linux系统WordPressIT 杂谈 首页 编程语言 Python Python Requests库:HTTP for H

2、umans Python Requests?HTTP for Humans 时间: 2014/12/30 | 分类: Python | 作者: 李坏 | 浏览:287 | 抢沙发 Python标准库中用来处理HTTP的模块是urllib2,不过其中的API太零碎了,requests是更简单更人性化的第三 方库。 用pip下载: pip install requests 或者git: git clone git:/ ? GET方法 李坏博客 ?. . open in browser PRO version Are you a developer? Try out the HTML to PDF

3、API import requests r = requests.get(https:/ POST方法: r = requests.post(“http:/httpbin.org/post“) 也可以使用其它方法: r = requests.put(“http:/httpbin.org/put“) r = requests.delete(“http:/httpbin.org/delete“) r = requests.head(“http:/httpbin.org/get“) r = requests.options(“http:/httpbin.org/get“) 也可以将请求方法放在参数中

4、: import requests req = requests.request(GET, http:/httpbin.org/get) ? 1.如果要将参数放在url中传递,使用params参数,可以是字典或者字符串: payload = key1: value1, key2: value2 r = requests.get(“http:/httpbin.org/get“, params=payload) r.url uhttp:/httpbin.org/get?key2=value2 boundary=e0f9ff1303b841498ae53a903f27e5 65“, “Host“:

5、“httpbin.org“, “User-Agent“: “python-requests/2.2.1 CPython/2.7.3 Windows/7“, , “url“: “http:/httpbin.org/post“ 一次性上传多个文件: url = http:/httpbin.org/post open in browser PRO version Are you a developer? Try out the HTML to PDF API multiple_files = (images, (foo.png, open(foo.png, rb), image/png) , (im

6、ages, (bar.png, open(bar.png, rb), image/png) r = requests.post(url, files=multiple_files) r.text . files: images: data:image/png;base64,iVBORw Content-Type: multipart/form-data; boundary=3131623adb2043caaeb5538cc7aa0b3a , . ?Headers import json url = https:/ payload = some: data headers = content-t

7、ype: application/json r = requests.post(url, data=json.dumps(payload), headers=headers) Response? 获取unicode字符串,会自动根据响应头部的字符编码(r.encoding)进行解码,当然也可以自己设定 r.encoding: r = requests.get(https:/ r.text u“message“:“Hello there, wayfaring stranger. 获取bytes字符串,会自动解码gzip和deflate数据: r.content open in browser P

8、RO version Are you a developer? Try out the HTML to PDF API “message“:“Hello there, wayfaring stranger. 要存储web图片,可以: from PIL import Image from StringIO import StringIO i = Image.open(StringIO(r.content) 可以解码json对象: r.json() udocumentation_url: uhttps:/developer. 返回raw response,需要在requests请求中将stream

9、设为True: r = requests.get(https:/ stream=True) r.raw r.raw.read(10) x1fx8bx08x00x00x00x00x00x00x03 如果不想一次性处理全部的数据,可以: tarball_url = https:/ r = requests.get(tarball_url, stream=True) if int(r.headerscontent-length) r = requests.get(http:/httpbin.org/get) r.status_code 200 获取响应headers: r.headers conte

10、nt-encoding: gzip, transfer-encoding: chunked, connection: close, server: nginx/1.0.4, x-runtime: 148ms, etag: “e1ca502697e5c9317743dc078f67693f“, content-type: application/json 获取发送的headers r.request.headers Accept-Encoding: identity, deflate, compress, gzip, Accept: */*, User-Agent: python-request

11、s/1.2.0 open in browser PRO version Are you a developer? Try out the HTML to PDF API Cookie 获取cookie,返回CookieJar对象: url = http:/ r = requests.get(url) r.cookies 将CookieJar转为字典: requests.utils.dict_from_cookiejar(r.cookies) BAIDUID: 84722199DF8EDC372D549EC56CA1A0E2:FG=1, BD_HOME: 0, BDSVRTM: 0 将字典转为C

12、ookieJar: requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True) 上传自己设置的cookie,使用cookies参数,可以是字典或者CookieJar对象: url = http:/httpbin.org/cookies cookies = dict(cookies_are=working) r = requests.get(url, cookies=cookies) r.text “cookies“: “cookies_are“: “working“ 如果需要在会话中保留cook

13、ie,需要用到后面要说的Session。 Redirection and History 可以用history属性来追踪redirection r = requests.get(http:/) r.url https:/ open in browser PRO version Are you a developer? Try out the HTML to PDF API r.status_code 200 r.history Session 要在会话中保留状态,可以使用request.Session()。 Session可以使用get,post等方法,Session对象在请求时允许你保留一定

14、的参数和自动设置cookie s = requests.Session() s.get(http:/httpbin.org/cookies/set/sessioncookie/123456789) #cookie保留在s中 r = s.get(“http:/httpbin.org/cookies“) #再次访问时会保留cookie print(r.text) # “cookies“: “sessioncookie“: “123456789“ 也可以自己设置headers,cookies: s = requests.Session() s.auth = (user, pass) s.headers.update(x-test: true) s.get(http:/httpbin.org/headers, headers=x-test2: true) # x-test a nd x-test2 都会被发送 ?Request 可以在发送request前做些额外的设定 from requests import Request, Session s = Session() r

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号