import requests
import json # 1、HTTP方法
requests.get('https://github.com/timeline.json') #GET请求
requests.post('http://httpbin.org/post') #POST请求
requests.put('http://httpbin.org/put') #PUT请求
requests.delete('http://httpbin.org/delete') #DELETE请求
requests.head('http://httpbin.org/get') #HEAD请求
requests.options('http://httpbin.org/get') #OPTIONS请求 # 2、Make a Request
url = 'http://www.baidu.com'
req = requests.get(url)
print(req.text) # 3、response属性
import requests
response = requests.get('http://www.baidu.com/s', params={'wd': 'python'}) # GET参数实例
print(response.url)
print(response.text) #返回的内容,字符串形式
print(response.status_code) #返回码
print(response.content) #返回的内容,二进制形式
print(response.headers)
print(response.headers['content-type'])
print(response.headers.get('content-type'))
print(response.encoding)
print(response.json())
response.raise_for_status() #抛出异常 非200响应 # 4、get带参数
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=payload)
print(r.url)
print(r.text) # 5、数据流方式读取结果
r = requests.get('https://api.github.com/events', stream=True)
print(r.raw) #不解码
print(r.raw.read(10))
with open('output.txt', 'wb') as fd:
for chunk in r.iter_content(chunk_size=128): #解码
fd.write(chunk) # 6、自定义头信息
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'} # 字符串,字节串,Unicode
r = requests.get(url, headers=headers) # 7、post数据:字典
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print(r.text) # 8、post数据:列表
payload = (('key1', 'value1'), ('key1', 'value2'))
r = requests.post('http://httpbin.org/post', data=payload)
print(r.text) # 9、post数据:字符串
url = 'http://httpbin.org/post'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload))
print(r.text) # 10、post数据:字符串
url = 'http://httpbin.org/post'
payload = {'some': 'data'}
r = requests.post(url, json=payload)
print(r.text) # 11、上传多部分编码的文件
url = 'http://httpbin.org/post'
files = {'file': open('a.xls', 'rb')}
r = requests.post(url, files=files)
print(r.text) # 12、上传多部分编码的文件
url = 'http://httpbin.org/post'
files = {'file': ('a.xls', open('a.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': ''})}
r = requests.post(url, files=files)
print(r.text) # 13、字符串当文件发
url = 'http://httpbin.org/post'
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files)
print(r.text) # 14、cookies
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
print(r.text) # 15、cookies
jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
url = 'http://httpbin.org/cookies'
r = requests.get(url, cookies=jar)
print(r.text) # 16、timeout
requests.get('http://github.com', timeout=0.001)

python3 requests模块 基本操作的更多相关文章

  1. python3: requests模块的使用;

    requests库常用于http请求,可以很方便对网页进行爬取: 主要方法(七个): 方法 解释 requests.request() 构造一个请求,支持以下各种方法 requests.get() 获 ...

  2. python3 requests模块

    一.Requests用法: 1.发送请求: 1).请求类型:req_obj = requests.get("https://www.baidu.com")requests支持多种请 ...

  3. python3:requests模块-写了一点

    使用requests,它的七个主要方法,在这里只讲两个:get.post >>> import requests >>> r=requests.get(" ...

  4. python3 requests 模块 json参数和data参数区别

    json 表示使用application/json方式提交请求 data 使用application/form-urlencode方式提交请求

  5. [实战演练]python3使用requests模块爬取页面内容

    本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...

  6. python3使用requests模块完成get/post/代理/自定义header/自定义Cookie

    一.背景说明 http请求的难易对一门语言来说是很重要的而且是越来越重要,但对于python一是urllib一些写法不太符合人的思维习惯文档也相当难看,二是在python2.x和python3.x中写 ...

  7. Python3:Requests模块的异常值处理

    Python3:Requests模块的异常值处理 用Python的requests模块进行爬虫时,一个简单高效的模块就是requests模块,利用get()或者post()函数,发送请求. 但是在真正 ...

  8. (转)Python3之requests模块

    原文:https://www.cnblogs.com/wang-yc/p/5623711.html Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另 ...

  9. Python3之requests模块

    Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务. 发送G ...

随机推荐

  1. 安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.3引发的血案

    1.下载了一个开源项目,是用netcore开发的 2.VS2015打不开解决方案 3.于是安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.3 4.安装成功,项目顺利 ...

  2. c# 委托 Predicate的使用示例

    一.说明 委托Predicate 可以有参数(比如下面的示例),也可以不带参数,委托Predicate是返回固定值bool值的委托 二.示例代码(控制台程序) using System; using ...

  3. react遇到的各种坑

    标签里用到<label for>的,for 要写成htmlFor 标签里的class要写成className 组件首字母一定要大写 单标签最后一定要闭合 如果html里要空格转义, 注意不 ...

  4. ElasticSearch(八)关于document的一些知识点

    先查看一条数据: GET /ecommerce/product/5 { "_index" : "ecommerce", "_type" : ...

  5. TTimer很特殊

    TTimer = class(TComponent) private FInterval: Cardinal; FWindowHandle: HWND; FOnTimer: TNotifyEvent; ...

  6. 最大流EK算法

    给定一个有向图G=(V,E),把图中的边看作 管道,每条边上有一个权值,表示该管道 的流量上限.给定源点s和汇点t,现在假设 在s处有一个水源,t处有一个蓄水池,问从 s到t的最大水流量是多少? 网络 ...

  7. MysqlNDB集群配置与管理

    为了避免不必要的资源分配,默认情况下是不启动ndbcluster引擎. 在管理节点,配置config.ini,注意请将空的[MYSQLD]的数量>2倍的sql节点数 当config.ini发生变 ...

  8. 常用的Css命名方式

    常用的Css命名方式: CSS命名规范: 1.文件命名规范 全局样式:global.css: 框架布局:layout.css: 字体样式:font.css: 链接样式:link.css: 打印样式:p ...

  9. PL/SQL DEVELOPER执行计划的查看

    这里,我学到的一个很重要的东西,就是用PL/SQL DEVELOPER去看一条SELECT语句的执行计划,执行计划里面可以看到这条SELECT语句的开销.I/O操作开销等数值,可以很清晰地看到语句各个 ...

  10. TCP与HTTP连接管理

    一. HTTP事务时延原因(HTTP权威指南 P86) 1.客户端首先需要根据URI确定WEB服务器的IP和端口号, 那么DNS解析上花的时间会很多(大多数HTTP客户端会有一个小的DNS缓存)   ...