博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 网络请求类库 requests 使用
阅读量:6992 次
发布时间:2019-06-27

本文共 3134 字,大约阅读时间需要 10 分钟。

python 网络请求类库 requests 使用

requests是 为python封装的强大 REST 操作类库

  • github
  • 官网 

1: 安装,请使用 pip,或是 easy_install 工具

     sudo pip install requests

2: 使用 先

    import requests

   

#coding=utf-8#要加上编码设置,不然编译不通过,这是python2的问题'''Created on 2014年4月22日@author: dev.keke@gmail.com'''#测试 request库环境,python2.7环境import requests   #GET 请求r = requests.get('http://httpbin.org/get')print 'GET:',r.text#POST 请求r = requests.post('http://httpbin.org/post')print 'POST',r.text#GET 带参数请求,pararms = {
'key1':'value1','key2':'value2'}r = requests.get('http://httpbin.org/get',params=pararms)print 'GET Pararms',r.url,r.text#POST 带参数请求,parar={
'key1':'value1','key2':'value2'}r = requests.post('http://httpbin.org/post',params = parar)print 'POST Pararms:',r.url,r.text#改变响应数据的编码 r.encoding = 'ISO-8859-1'#取得响应的二进制数据r.content#取得json数据,如果数据json处理失败,会抛出异常r.json()#响应原始数据内容,注意设置 r = requests.get('https://github.com/timeline.json', stream=True) tream = Truer.raw #原数据内容

3:一些更为复杂的操作

   定制请求头

>>> 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请求

>>> payload = {
'key1': 'value1', 'key2': 'value2'}>>> r = requests.post("http://httpbin.org/post", data=payload)>>> print r.text{ ... "form": { "key2": "value2", "key1": "value1" }, ...}
>>> import json>>> url = 'https://api.github.com/some/endpoint'>>> payload = {
'some': 'data'}>>> r = requests.post(url, data=json.dumps(payload))

上传文件

>>> url = 'http://httpbin.org/post'>>> files = {
'file': open('report.xls', 'rb')}>>> r = requests.post(url, files=files)>>> r.text{ ... "files": { "file": "
" }, ...}
>>> url = 'http://httpbin.org/post'>>> files = {
'file': ('report.xls', open('report.xls', 'rb'))}>>> r = requests.post(url, files=files)>>> r.text{ ... "files": { "file": "
" }, ...}
>>> url = 'http://httpbin.org/post'>>> files = {
'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}>>> r = requests.post(url, files=files)>>> r.text{ ... "files": { "file": "some,data,to,send\\nanother,row,to,send\\n" }, ...}

响应状态码:

内置状态码:

>>> r.status_code == requests.codes.okTrue

根据状态码抛出异常

>>> bad_r = requests.get('http://httpbin.org/status/404')>>> bad_r.status_code404>>> bad_r.raise_for_status()Traceback (most recent call last):  File "requests/models.py", line 832, in raise_for_status    raise http_errorrequests.exceptions.HTTPError: 404 Client Error

查看响应头

>>> r.headers{    'status': '200 OK',    'content-encoding': 'gzip',    'transfer-encoding': 'chunked',    'connection': 'close',    'server': 'nginx/1.0.4',    'x-runtime': '148ms',    'etag': '"e1ca502697e5c9317743dc078f67693f"',    'content-type': 'application/json; charset=utf-8'}

设置请求超时时间

>>> requests.get('http://github.com', timeout=0.001)Traceback (most recent call last):  File "
", line 1, in
requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)

 

参考:

转载于:https://www.cnblogs.com/cocoajin/p/3680536.html

你可能感兴趣的文章
nginx安装
查看>>
angularjs 利用filter进行表单查询及分页查询
查看>>
stack
查看>>
SCAU 8588 表达式求值
查看>>
OD使用教程5 - 调试篇05|解密系列
查看>>
kindeditor 操作时同步到textarea
查看>>
修改已经释放了的请求号
查看>>
重写和强制转换再调用能编译但不能运行
查看>>
logging ,re 模块
查看>>
Android入门之GridView(表格控件)
查看>>
JavaScript基础篇
查看>>
Cesium 加载天地图
查看>>
Centos7中安装最新版maven3.5.0
查看>>
python学习之老男孩python全栈第九期_数据库day003 -- 作业
查看>>
深度优先遍历
查看>>
常用类型转换 一.常用int和string类型转换
查看>>
Ext Js简单Grid分页及选择器的使用
查看>>
slice 定义和用法
查看>>
分类游戏 结构体
查看>>
导出、恢复、上传镜像
查看>>