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. Erlang Shell调试网络程序真方便

    Erlang的shell功能强大,这里我将它当成我的客户端.可以动态的输入你需要发送的内容,也可以动态的接收内容,就像调试器一样,在开发过程中起到很重要的作用.具体使用方式如下: C:\Documen ...

  2. SAM4E单片机之旅——1、LED闪烁之空循环

    最近因为导师要写一本关于SAME4单片机的书籍,而我也作为一个嵌入式的初学者看了这本书.现在也让我写写几个小的程序,做做示例.既然写了文档之类的,就发到博客上来吧. 目前关于这芯片能参考的书籍大概就只 ...

  3. EasyDarwin Streaming Server对Task的调用方法

    我们在EasyDarwin流媒体服务器的二次开发过程中,经常会需要定义自己的Task类,例如在EasyDarwin中,RTSPSessioin.HTTPSession.RTCPTask等,都是Task ...

  4. RTSP流媒体转发服务器源码

    最新EasyDarwin已经支持海康.大华等标准RTSP/RTP协议的转发,代码及使用方法参看:用Darwin开发RTSP级联服务器(拉模式转发)http://blog.csdn.net/xiejia ...

  5. contentprovider 实例

    Provider端 public class PersonProvider extends ContentProvider { //用来存放所有合法的Uri的容器 private static Uri ...

  6. alsa 编程

    ALSA(Advanced Linux Sound Architecture)是由内核驱动,标准的API库和一系列实用程序组成.因为涉及到版权和BUG的问题Linux 2.6内核抛弃了旧的OSS,AL ...

  7. tomcat正常启动,但是java项目没有启动原因

    右键项目,选择properties,查看该属性配置的是否正确

  8. 第二篇:python基础之核心风格

    阅读目录 一.语句和语法 二.变量定义与赋值 三.内存管理 内存管理: 引用计数: 简单例子 四.python对象 五.标识符 六.专用下划线标识符 七.编写模块基本风格 八.示范 一.语句和语法 # ...

  9. 英语发音规则---s发/s/的读音规则

    英语发音规则---s发/s/的读音规则 一.总结 一句话总结:字母s的读音有/s/./z/./ʃ/./{/这几种,下面主要讲讲发/s/音的几条规则. 字母s的读音有/s/./z/./ʃ/./{/这几种 ...

  10. linux应用之mysql数据库的安装及配置(centos)

    CentOS下Mysql数据库的安装与配置   如果要在Linux上做j2ee开发,首先得搭建好j2ee的开发环境,包括了jdk.tomcat.eclipse的安装(这个在之前的一篇随笔中已经有详细讲 ...