python第三方库学习(2):requests
Make a Request
r = requests.get('https://github.com/timeline.json')
Passing Parameters In URLs
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
print(r.url)
Response Content
>>> r = requests.get('https://github.com/timeline.json')
>>> r.text
u'[{"repository":{"open_issues":0,"url":"https://github.com/...
>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1'
Binary Response Content
>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
>>> from PIL import Image
>>> from StringIO import StringIO
>>> i = Image.open(StringIO(r.content))
JSON Response Content
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/..
Raw Response Content
>>> 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'
Custom Headers
>>> 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)
More complicated POST requests
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print r.text
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}
POST a Multipart-Encoded File
>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}
>>> r = requests.post(url, files=files)
>>> r.text
{
...
"files": {
"file": "<censored...binary...data>"
},
...
}
Response Status Codes
>>> r = requests.get('http://httpbin.org/get')
>>> r.status_code
200
Response Headers
>>> r.headers
{
'content-encoding': 'gzip',
'transfer-encoding': 'chunked',
'connection': 'close',
'server': 'nginx/1.0.4',
'x-runtime': '148ms',
'etag': '"e1ca502697e5c9317743dc078f67693f"',
'content-type': 'application/json'
}
Cookies
>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working')
>>> r = requests.get(url, cookies=cookies)
>>> r.text
'{"cookies": {"cookies_are": "working"}}'
Redirection and History
>>> r = requests.get('http://github.com', allow_redirects=False)
>>> r.status_code
301
>>> r.history
[]
Timeouts
>>> requests.get('http://github.com', timeout=0.001)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)
python第三方库学习(2):requests的更多相关文章
- python第三方库requests简单介绍
一.发送请求与传递参数 简单demo: import requests r = requests.get(url='http://www.itwhy.org') # 最基本的GET请求 print(r ...
- python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑
python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件?当我们点开下载页时, 一般 ...
- python第三方库——xlrd和xlwt操作Excel文件学习
python第三方库——xlrd和xlwt操作Excel文件学习 1安装: C:\Users\Lenovo>pip install xlwtCollecting xlwt Downloadin ...
- Python第三方库资源
[转载]Python第三方库资源 转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github ...
- 3、python第三方库的安装方式
在学习Python过程中,经常要用到很多第三方库,面对各种不同情况,Python为我们提供了多种安装方法,这里主要介绍三种 方法:pycharm在线安装.pip在线安装(强烈推荐).离线安装. 方式一 ...
- python文件打开模式&time&python第三方库
r:以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. w:打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. a:打开一个文件用于追加.如果该文件已存在 ...
- [爬虫]Windows下如何安装python第三方库lxml
lxml是个非常有用的python库,它可以灵活高效地解析xml与BeautifulSoup.requests结合,是编写爬虫的标准姿势. 但是,当lxml遇上Windows,简直是个巨坑.掉在安装陷 ...
- 【Python基础】安装python第三方库
pip命令行安装(推荐) 打开cmd命令行 安装需要的第三方库如:pip install numpy 在安装python的相关模块和库时,我们一般使用“pip install 模块名”或者“pyth ...
- python第三方库自动安装脚本
#python第三方库自动安装脚本,需要在cmd中运行此脚本#BatchInstall.pyimport oslibs = {"numpy","matplotlib&qu ...
随机推荐
- 二分查找-python
约12年年底的时候,接触了python不到半年的样子,入门是直接实现GUI测试case的.今天面试地平线机器人,发现忘得差不多了- -. 当时的问题是这样的 写一个二分查找是实现,我好像不记得二分查找 ...
- UML大战需求分析阅读笔记1
UML这三个字母的全称是Unified Modeling Language,直接翻译就是统一建模语言,简单地说就是一种有特殊用途的语言.你可能会问:这明明是一种图形,为什么说是语言呢?伟大的汉字还不是 ...
- oracle 正则表达式
1.获取不包含 欧洲|*北美|*中国|*欧拉非 字符串的行. SELECT/* count(*)*/* FROM table t WHERE NOT regexp_like(t.xxxx, '( ...
- SQL Server查询结果插入表
a) 插入新表 select * into newtable from table b) 插入已经存在的表 insert into table select * from table2 where.. ...
- UNICODE 7.0定义的表情符
td.graph { font-size:24px; } UNICODE 7.0定义了78个表情符,从0x1F600到0x1F64F(其中0x1F643和0x1F644没有定义).下表中列出了这些表情 ...
- Redis学习笔记-进阶
Redis持久化方案 redis有rdb和aof两种持久化方案 1)rdb方式 当符合一定条件时会自动将内存中的所有数据执行快照操作并存储到硬盘上 默认存储在redis根目录的dump.rdb文件中, ...
- keepalived+nginx高可用负载均衡环境搭建
上篇说道keepalived的环境搭建,本来keepalived结合lvs更有优势,但是也可以结合nginx来使用.下面接着说下nginx的环境搭建 环境信息: nginx(master) 192. ...
- mysql workbench
下载地址:http://dev.mysql.com/downloads/ 详情:http://baike.baidu.com/link?url=sWV3b2pWdr8cvCxEZYrB9CzLD9Bl ...
- 第13章 .NET应用程序配置
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...
- hdu 1455 Sticks(dfs+剪枝)
题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...