python requests 库使用小结(一)
来源:互联网 发布:所有网络女歌手的名字 编辑:程序博客网 时间:2024/04/23 17:39
一 安装:
推荐方式:
pip install requests
二 快速功能预览:
1 发送一个请求可以是get put post head delete options
>>> import requests>>> r = requests.get('http://blog.csdn.net/hanglinux')>>> r = requests.put('http://httpbin.org/put', data = {'key':'value'})>>> r = requests.delete('http://httpbin.org/delete')>>> r = requests.head('http://httpbin.org/get')>>> r = requests.options('http://httpbin.org/get')
2传递参数
可以向url传递key/value 参数:
>>> payload = {'key1': 'value1', 'key2': 'value2'}>>> r = requests.get('http://httpbin.org/get', params=payload)
3 获得请求的内容:
>>> import requests>>> r = requests.get('http://blog.csdn.net/hanglinux')>>> r.text
同时可以查看和修改编码:
>>> r.encoding'utf-8'>>> r.encoding = 'ISO-8859-1'
格式化成json形式:
r.json()
4 设定请求头
特别是在写爬虫的时候,如果没有请求头很有可能被网站屏蔽访问,添加请求头的方式:
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Accept-Encoding":"gzip, deflate, sdch, br","Accept-Language":"en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4","cache-control":"max-age=0","Referer":"http://www.example.com/","User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"}url=‘http://blog.csdn.net/hanglinux’r = requests.get(url, headers=headers)
5 更加复杂的POSt请求:
>>> payload = {'key1': 'value1', 'key2': 'value2'}>>> r = requests.post("http://httpbin.org/post", data=payload)>>> print(r.text){..."form": {"key2": "value2","key1": "value1"},...}
发送json 格式的参数,这里以github为例子:
>>> import json>>> url = 'https://api.github.com/some/endpoint'>>> payload = {'some': 'data'}>>> r = requests.post(url, data=json.dumps(payload))
POST 方式发送文件:
>>> url = 'http://httpbin.org/post'>>> files = {'file': open('report.xls', 'rb')}>>> r = requests.post(url, files=files)>>> r.text{..."files": {"file": "<censored...binary...data>"},...}
提示:
强力推荐打开文件的时候使用binary mode。这时因为requests会尝试提供Content-Length 的bytes值作wei
文件大小,如果用text mode打开文件,可能发生错误.
6 请求的相应状态:
>>> r = requests.get('http://httpbin.org/get')>>> r.status_code200
>>> r = requests.get('http://httpbin.org/get')
>>> r.status_code
200
同时requests内建了状态对象,提供判断使用:
>>> r.status_code == requests.codes.ok
True
7 Cookies:
如果响应内容里面包含Cookie,可以很容易的访问到:
>>> url = 'http://example.com/some/cookie/setting/url'>>> r = requests.get(url)>>> r.cookies['example_cookie_name']'example_cookie_value'
同时也可以发送cookie:
>>> url = 'http://httpbin.org/cookies'>>> cookies = dict(cookies_are='working') 需要转换成字典的形式>>> r = requests.get(url, cookies=cookies)>>> r.text'{"cookies": {"cookies_are": "working"}}'
8.请求超时时间
可以设置请求的超时时间,用timeout 参数,
requests.get('http://github.com', timeout=0.001)
9 错误和异常处理:
当出现了网络问题,比如 dns错误 拒绝访问等 requests将跑出一个ConnectionError异常到上层.
超时的时候会抛出:Timeout异常.
如果HTTP返回不成功的状态,将返回HTTPError错误.
阅读全文
0 0
- python requests 库使用小结(一)
- python requests 库使用小结(二)
- Python从零开始写爬虫(一)requests库使用
- Python requests库使用实例
- python的 requests库使用
- python requests库翻译(一)
- Python模拟登陆(使用requests库)
- Python Requests库简明使用教程
- Python库Requests的基础使用
- Python Requests库简明使用教程
- python-requests库的基本使用教程
- python中的requests使用
- Python-requests的使用
- Python 爬虫实战(一):使用 requests 和 BeautifulSoup
- python的requests类库(一)requests库和urllib包对比:一个简单get请求
- Python利用Requests库写爬虫(一)
- Python爬虫利器一之Requests库的用法
- Python-Requests库进行接口测试(一)
- 自定义按钮
- 利用Flume将MySQL表数据准实时抽取到HDFS
- 数据压缩实验四 DPCM 压缩系统的实现和分析
- ES7的Async/Await
- 使用 RequireJS 加载 AngularJS
- python requests 库使用小结(一)
- matplotlib作图布局
- Android 自定义UI-单点触摸-多点触摸
- springmvc几种传值方式
- Kotlin Reference (十一) 泛型、数组型变、泛型型变、泛型约束
- 中的PLL、MPLL、UPLL、FCLK、HCLK、PCLK的作用概述
- 解读关键字volatile
- Rust: Vec用法及其它
- 使用python比较两个文件的不同之处