Python基础笔记系列十二:requests模块的简单应用
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!!
- httpbin
httpbin这个网站能测试 HTTP 请求和响应的各种信息,
比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,
对 web 开发和测试很有帮助。它用 Python + Flask 编写,是一个开源项目。
官方网站:http://httpbin.org/
开源地址:https://github.com/Runscope/httpbin - get请求
一、简单get请求数据import requests
get_url = 'http://www.httpbin.org/get'
r = requests.get(get_url) #发送get请求
print r
print help(r)
print r.text #接收的内容in bytes
print r.content #接收的内容in unicode二、使用python抓去百度主页
import requests
r = requests.get('http://www.baidu.com')
print r.content
#打开一个文件,并存入网页信息
with open('baidu.html','w') as fil:
fil.write(r.content)三、get传参
#可以先通过help函数来查看下get方法的文档说明
import requests
# print help(requests.get)
get_url = 'http://www.httpbin.org/get'
myparams={'qq':''}
# r = requests.get(url=get_url,params=myparams)
r = requests.get(get_url,myparams)
print r.url #请求的url,拼接url和参数
print r.content #返回的数据 - post请求
一、发送请求体
例子:import requests
#先来查看下post方法的文档使用说明
# print help(requests.post)
#通过data来传参,相当于传的表单,在返回的数据中可以看出
post_url = 'http://www.httpbin.org/post'
myData={'qq':'','cnblog':'hyyq'}
r = requests.post(url = post_url,data = myData)
print r.url #请求的url,请求体就是参数
print r.content #返回的数据输出:
http://www.httpbin.org/post
{
"args": {},
"data": "",
"files": {},
"form": {
"cnblog": "hyyq",
"qq": "34782655"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "www.httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"json": null,
"origin": "183.230.42.10",
"url": "http://www.httpbin.org/post"
} [Finished in 1.3s]二、发送json数据
例子:import requests,json
post_url = 'http://www.httpbin.org/post'
myData={'qq':'','cnblog':'hyyq'}
r = requests.post(url = post_url,json = myData)
# r = requests.post(url = post_url,data = json.dumps(myData))
print r.url #请求的url,发送json数据
print r.content #返回的数据输出:
http://www.httpbin.org/post
{
"args": {},
"data": "{\"qq\": \"34782655\", \"cnblog\": \"hyyq\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "36",
"Content-Type": "application/json",
"Host": "www.httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"json": {
"cnblog": "hyyq",
"qq": "34782655"
},
"origin": "183.230.42.10",
"url": "http://www.httpbin.org/post"
} [Finished in 1.0s] - 上传文件
例子:#上传文件
import requests
# print help(requests)
post_url = 'http://www.httpbin.org/post'
files = {'file':open('yyc.txt','rb')} #这里rb是以读的方式用二进制格式打开
r = requests.post(post_url,files=files)
print r.content输出:
{
"args": {},
"data": "",
"files": {
"file": "data:application/octet-stream;base64,xOO6ww=="
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "147",
"Content-Type": "multipart/form-data; boundary=8bffc774646942deaa8a314f8675d879",
"Host": "www.httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"json": null,
"origin": "183.230.42.10",
"url": "http://www.httpbin.org/post"
} [Finished in 1.0s]
Python基础笔记系列十二:requests模块的简单应用的更多相关文章
- Python基础笔记系列十:模块
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 模块 #1.类比于java中的jar包,模块能让你能够有逻辑地组织你的Py ...
- Python基础笔记系列十四:python无缝调用c程序
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! python语言可以对c程序代码进行调用,以弥补python语言低性能的缺 ...
- Python基础笔记系列十一:标准输入输出、文件读写和指针等操作
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 标准输入输出一.输入 在sublime中这个时候需要安装SublimeRE ...
- Python基础笔记系列一:基本工具与表达式
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 工具基础(Windows系统下)传送门:Python基础笔记系列四:工具的 ...
- (Python基础教程之十二)Python读写CSV文件
Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...
- python学习笔记(十二)python操作redis
1.python要操作redis 首先需要安装redis模块,然后导入才能使用 安装:pip install redis 导入:import redis 2.连接redis r = redis.Red ...
- python学习笔记:网络请求——requests模块
上面讲过的urllib模块太麻烦了,还有一个比较方便的模块,就是requests模块,好用到你怀疑人生·^_^,一定要会哦 需要安装,pip install requests即可,下面是request ...
- Python基础笔记系列二:分支和循环
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 分支:即是if-else和if-elif-else语句 循环:即是whil ...
- python学习笔记-(十二)scoket编程基础
socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Un ...
随机推荐
- 【opencv】cv::Mat转std::vector<cv::Point2d> (注意两容器中数据类型的一致性)
获取cv::Mat大小: mymat.size() 获取cv::Mat指定位置的值:需指定数据类型,且注意数据类型应与存入时的数据类型一致,否则会导致不抛出异常的数据错误 mymat.at<,i ...
- yarn的使用
yarn 的安装 npm install -g yarn yarn -version 查看yarn是否安装成功 一.首先需要了解的命令 npm install === yarn —— install ...
- django URL的补充 默认值 传多个参数
url 后面还可以加上默认值 默认值 url(r'^index/', views.index, {'name': 'root'}), urls.py url对应关系 from django.conf. ...
- python web框架 django wsgi 理论
django wsgi python有个自带的wsgi模块 可以写自定义web框架 用wsgi在内部创建socket对象就可以了 自己只写处理函数就可以了django只是web框架 他也不负责写soc ...
- Elasticsearch.js 发布 —— 在Node.js和浏览器中调用Elasticsearch(1)
继PHP.Ruby.Python和Perl之后,Elasticsearch最近发布了Elasticsearch.js,Elasticsearch的JavaScript客户端库.可以在Node.js和浏 ...
- PAT 1120 Friend Numbers[简单]
1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...
- PAT 1069 The Black Hole of Numbers[简单]
1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...
- Selenium+Python学习之一
刚入门selenium+Python,实验成功之后,记录一下过程. 首先是在知乎上面看到一个关于selenium+python的示例,于是自己便尝试搭建环境上手实验. 按照作者的代码敲一遍之后执行,竟 ...
- ruby中的作用域
作用域(scope)指的是变量的可达性或可见性.不同类型的变量有不同的作用域规则.与self类似,作用域在程序的执行过程中也在不断的变化,也可以根据上下文推断出"谁在什么作用域中" ...
- 开源BBS论坛软件推荐
七款开源BBS论坛软件推荐(1) 本文介绍了七个开源的BBS论坛软件(在英文界一般叫做Forum).可能国内的朋友们比较熟悉Discuz!和PHPwind,但其实我们的选择还是很多的,而且下面介绍的这 ...