python--requests模块详解
GET请求
首先构造一个最简单的get请求,请求的链接为http://httpbin.org/get
import requests
2 r = requests.get("http://httpbin.org/get")
3 print(r.text)
#运行结果
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4",
"X-Amzn-Trace-Id": "Root=1-5f704dcf-78e431abe1d838c6c44e50ac"
},
"origin": "58.17.121.223",
"url": "http://httpbin.org/get"
}
可以发现我们成功的发起了get请求,并且返回结果中包括了请求头,URL,IP等信息
如果发起请求的URL地址需要参数,利用params这个参数
import requests
2 date = {
3 "name":"germey",
4 "age":22
5 }
6 r = requests.get("http://httpbin.org/get",params = date)
7
8 print(r.text)
#运行结果
{
"args": {
"age": "22",
"name": "germey"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4",
"X-Amzn-Trace-Id": "Root=1-5f704f27-854822ce60e1c5f94da41517"
},
"origin": "58.17.121.223",
"url": "http://httpbin.org/get?name=germey&age=22"
}
通过运行结果我们可以判断,请求的链接自动被构造成了:http://httpbin.org/get?age = 22&name= germey
添加headers
有些网站如果不传递headers则会被禁止访问,所以一般在发起请求之前我们都要进行UA伪装
POST请求
import requests
2 data = {"name":"germey","age":22}
3 r = requests.post("http://httpbin.org/post",data = data)
4 print(r.text)
#运行结果
{
"args": {},
"data": "",
"files": {},
"form": {
"age": "22",
"name": "germey"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "18",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4",
"X-Amzn-Trace-Id": "Root=1-5f70517c-43382ec6284d9ce2a3cd28f1"
},
"json": null,
"origin": "58.17.121.223",
"url": "http://httpbin.org/post"
}
其中form就是需要提交的数据
python--requests模块详解的更多相关文章
- Python—requests模块详解
1.模块说明 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支持使用co ...
- python time模块详解
python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明 一.简介 ...
- python docopt模块详解
python docopt模块详解 docopt 本质上是在 Python 中引入了一种针对命令行参数的形式语言,在代码的最开头使用 """ ""&q ...
- (转)python collections模块详解
python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...
- python pathlib模块详解
python pathlib模块详解
- Python Fabric模块详解
Python Fabric模块详解 什么是Fabric? 简单介绍一下: Fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率. 再具体点介绍一下,Fabri ...
- python time 模块详解
Python中time模块详解 发表于2011年5月5日 12:58 a.m. 位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...
- python常用模块详解
python常用模块详解 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用p ...
- python os模块详解
一.Python os模块(Linux环境) 1.1 执行shell命令 os.system('cmd') 执行命令不保存结果 os.popen('command') 执行后返回结果,使用.read( ...
- Python ZipFile模块详解(转)
Python zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下,我们只需要使用这两个cl ...
随机推荐
- sdut2879 枚举起点DP
这个题和乌龟棋之类的DP差不多要学会缩减状态 就是,,我们只需枚举当前这个人是谁,选什么颜色,A用了多少,B用了多少 C用了多少我们就不用枚举了,知道选了多少人,A,B用了多少,你还不知C用了多少么, ...
- C++ Primer笔记
C++ Primer笔记 ch2 变量和基本类型 声明 extern int i; extern int i = 3.14;//定义 左值引用(绑定零一变量初始值,别名) 不能定义引用的引用:引用必须 ...
- SVG path d Attribute
Scalable Vector Graphics (SVG) 1.1 (Second Edition) W3C Recommendation 16 August 2011 http://www.w3. ...
- Windows 常用键盘快捷键:
键盘快捷键 通过使用键盘快捷键可以节省时间. Windows 和 Mac 的键盘快捷键 在现代操作系统中和计算机软件程序中,键盘快捷键经常被使用. 使用键盘快捷键能帮您节省很多时间. 基本的快捷键 描 ...
- Android Activity 与 WebView 页面线程不一致 bug
Android Activity 与 WebView 页面线程不一致 bug refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
- MBP 2018
MBP 2018 touch pad MacBook Pro 如何调节键盘背光 https://support.apple.com/zh-cn/HT202310 F6 & F5 如何清洁 Ma ...
- [转]什么是 C 和 C ++ 标准库?
转载地址:https://www.cnblogs.com/findumars/p/9000371.html 简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的.我已 ...
- AI数学基础之:奇异值和奇异值分解
目录 简介 相似矩阵 对角矩阵 可对角化矩阵 特征值 特征分解 特征值的几何意义 奇异值 Singular value 奇异值分解SVD 简介 奇异值是矩阵中的一个非常重要的概念,一般是通过奇异值分解 ...
- 【Notes】现代图形学入门_01
跟着闫令琪老师的课程学习,总结自己学习到的知识点 课程网址GAMES101 B站课程地址GAMES101 课程资料百度网盘[提取码:0000] 计算机图形学概述 计算机图形学是一门将模型转化到屏幕上图 ...
- MySQL学习笔记(五)
倒数第二天!冲冲冲!!! 一.索引 一个表里面可以有多个索引. 1. 索引的作用:约束与加速查找 无索引:从前到后依次查找 有索引:会为索引列创造一个额外文件(以某种格式存储).在使用索引进行查找时, ...