python3 requests模块 基本操作
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模块 基本操作的更多相关文章
- python3: requests模块的使用;
requests库常用于http请求,可以很方便对网页进行爬取: 主要方法(七个): 方法 解释 requests.request() 构造一个请求,支持以下各种方法 requests.get() 获 ...
- python3 requests模块
一.Requests用法: 1.发送请求: 1).请求类型:req_obj = requests.get("https://www.baidu.com")requests支持多种请 ...
- python3:requests模块-写了一点
使用requests,它的七个主要方法,在这里只讲两个:get.post >>> import requests >>> r=requests.get(" ...
- python3 requests 模块 json参数和data参数区别
json 表示使用application/json方式提交请求 data 使用application/form-urlencode方式提交请求
- [实战演练]python3使用requests模块爬取页面内容
本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...
- python3使用requests模块完成get/post/代理/自定义header/自定义Cookie
一.背景说明 http请求的难易对一门语言来说是很重要的而且是越来越重要,但对于python一是urllib一些写法不太符合人的思维习惯文档也相当难看,二是在python2.x和python3.x中写 ...
- Python3:Requests模块的异常值处理
Python3:Requests模块的异常值处理 用Python的requests模块进行爬虫时,一个简单高效的模块就是requests模块,利用get()或者post()函数,发送请求. 但是在真正 ...
- (转)Python3之requests模块
原文:https://www.cnblogs.com/wang-yc/p/5623711.html Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另 ...
- Python3之requests模块
Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务. 发送G ...
随机推荐
- react遇到的各种坑
标签里用到<label for>的,for 要写成htmlFor 标签里的class要写成className 组件首字母一定要大写 单标签最后一定要闭合 如果html里要空格转义, 注意不 ...
- undefined reference to '__android_log_print'解决方案
1:在源程序中添加头文件 #include <cutils/log.h> 2:在Android.mk中添加 LOCAL_SHARED_LIBRARIES := \ libutils \ l ...
- hdu1198 Farm Irrigation —— dfs or 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 dfs: #include<cstdio>//hdu1198 dfs #includ ...
- sublime text3 3176 注册码 License
注册码 sgbteam Single User License EA7E-1153259 8891CBB9 F1513E4F 1A3405C1 A865D53F 115F202E 7B91AB2D 0 ...
- 并不对劲的bzoj1095:p2056:[ZJOI2007]捉迷藏
题目大意 给一\(n\)(\(n\leq10^5\))个点的一棵树,每个点有可能是黑色或白色,一开始所有点都是黑色,支持以下两种操作: 1.改变一个点的颜色 2.询问最远的黑色点对的距离 题解 据说是 ...
- bzoj 4503 两个串 —— FFT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4503 推式子即可: 不知怎的调了那么久,应该是很清晰的. 代码如下: #include< ...
- 洛谷P3385判负环——spfa
题目:https://www.luogu.org/problemnew/show/P3385 两种方法,dfs和bfs: 一开始写的dfs,要把dis数组初值赋成0,这样从一个连着负边的点开始搜: 在 ...
- ubuntu下网络性能测试
iperf的主要功能 TCP 测量网络带宽 报告MSS/MTU值的大小和观测值 支持TCP窗口值通过套接字缓冲 当P线程或Win32线程可用时,支持多线程.客户端与服务端支持同时多重连接 UDP 客户 ...
- javascript闭包和闭包的几种写法和用法
什么是闭包 闭包,官方的解释是:一个拥有需要许多变量和绑定了这=这些变量的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.闭包的特点: 1 作为一个函数变量的引用,当函数返回时,其处于激活 ...
- 关于chrome被篡改主页修复方法
打开chrome的属性. 在目标框中追加以下内容 "PATH\chrome.exe" --flag-switches-begin --flag-switches-end about ...