python requests库的用法
参考 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
1.传递url参数
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload)
>>> print(r.url)
http://httpbin.org/get?key2=value2&key1=value1
还可以传递值为列表
>>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
>>> r = requests.get('http://httpbin.org/get', params=payload)
>>> print(r.url)
http://httpbin.org/get?key1=value1&key2=value2&key2=value3
2.响应内容
可以取服务器响应的内容,
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.text
u'{"message":"Hello there, wayfaring stranger. If you\u2019re reading
this then you probably didn\u2019t see our blog post a couple of years
back announcing that this API would go away: http://git.io/17AROg Fear
not, you should be able to get what you need from the shiny new Events
API instead.","documentation_url":"https://developer.github.com/v3/act
ivity/events/#list-public-events"}'
>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1' #改变编码方式
r.json() #JSON 响应内容
{u'documentation_url': u'https://developer.github.com/v3/activity/events
/#list-public-events', u'message': u'Hello there, wayfaring stranger.
If you\u2019re reading this then you probably didn\u2019t see our blog
post a couple of years back announcing that this API would go away: http
://git.io/17AROg Fear not, you should be able to get what you need from
the shiny new Events API instead.'}
3. 定制请求头
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}
>>> r = requests.get(url, headers=headers)
python requests库的用法的更多相关文章
- (转)Python爬虫利器一之Requests库的用法
官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...
- Python爬虫利器一之Requests库的用法
前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...
- Python:requests库、BeautifulSoup4库的基本使用(实现简单的网络爬虫)
Python:requests库.BeautifulSoup4库的基本使用(实现简单的网络爬虫) 一.requests库的基本使用 requests是python语言编写的简单易用的HTTP库,使用起 ...
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...
- python requests库学习笔记(上)
尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...
- Python——Requests库的开发者接口
本文介绍 Python Requests 库的开发者接口,主要内容包括: 目录 一.主要接口 1. requests.request() 2. requests.head().get().post() ...
- 使用python requests库写接口自动化测试--记录学习过程中遇到的坑(1)
一直听说python requests库对于接口自动化测试特别合适,但由于自身代码基础薄弱,一直没有实践: 这次赶上公司项目需要,同事小伙伴们一起学习写接口自动化脚本,听起来特别给力,赶紧实践一把: ...
- Python requests库的使用(一)
requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/z ...
- python爬虫---requests库的用法
requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下 ...
随机推荐
- 图像RGB格式与YUV格式互转
// rgb2yuv.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #define Level 256 //直方图bin数 #define ...
- Codeforces 932.F Escape Through Leaf
F. Escape Through Leaf time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- 安装好dashboard 登录出现错误
验证发生错误.请稍后再试一次. While turning SELinux off certainly does the trick, it is somewhat like using a sled ...
- linux中使用随机数
(1)单纯使用rand重复调用n次,就会得到一个0-RAND_MAX之间的伪随机数,如果需要调整范围,可以得到随机数序列后再进行计算.(2)单纯使用rand来得到伪随机数序列有缺陷,每次执行程序得到的 ...
- 前端PHP入门-013-变量作用域
目前,我们知道了几个不同的规矩: 函数定义时后括号里面接的变量是形式上的参数(形参),与函数体外的变量没有任何关系.仅仅是在函数内部执行 函数内声明的变量也与函数外的变量没关系. 但是,我们实际的处理 ...
- [DeeplearningAI笔记]序列模型2.1-2.2词嵌入word embedding
5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.1词汇表征 Word representation 原先都是使用词汇表来表示词汇,并且使用1-hot编码的方式来表示词汇 ...
- Vue.js随笔三(npm init webpack my-project指令安装失败解决方案)
如果没有安装淘宝给的镜像就先安装一下,指令如下,对!就是如此简单: npm install -g cnpm -registry=https://registry.npm.taobao.org 首先输入 ...
- goaccess日志分析
对于nginx日志分析,有很多工具,衡量好坏的标准大概就是三快:安装快,解析快,上手快.满足这三点的goaccess确实是居家必备良药. 话说这个标题其实有点委屈GoAccess了,它是一个日志分析工 ...
- 拓扑排序 最大字典序+优先队列 BZOJ 4010
http://www.lydsy.com/JudgeOnline/problem.php?id=4010 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory ...
- PHP扩展开发--02.包裹第三方的扩展
背景 也许最常见的PHP扩展是那些包裹第三方C库的扩展.这些扩展包括MySQL或Oracle的数据库服务库,libxml2的 XML技术库,ImageMagick 或GD的图形操纵库. 在本节中,我们 ...