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 ...
随机推荐
- Canvas & encryption image src
Canvas & encode image src 使用 Canvas 加密图片,防盗链 前端黑科技 https://telegram.org/ binary encode ??? https ...
- The best Fibonacci is achieved in js
The best Fibonacci is achieved in js the best realized by using js 斐波那契数列 "use strict"; /* ...
- foreign language learning
foreign language learning free online learning websites 多邻国 https://www.duolingo.com 忆术家 https://www ...
- 移动端 750px UI 设计稿
750px UI 设计稿 App 小程序 H5 rem & vh/vw 在移动端页面开发中,UI 一般会用750px(iphone 6)来出设计稿; 然后要求能够做到页面是自适应屏幕的,这种情 ...
- PAUL ADAMS ARCHITECT LTD:拜登上台后,美国房地产走势如何?
受全球经济危机影响,美国经济的复苏之路并不平坦,然而住宅房地产市场却是少数的例外,表现得非常亮眼.而随着美国拜登总统上台的临近,美国房产市场还会持续当前的发展吗?对于这个问题,近日,美国知名房地产公司 ...
- Java的super、this、重写
Java的super.this.重写 一.super的注意点: super调用父类的构造方法,必须在构造方法的第一个: super只能出现在子类的构造方法或者方法中: this和super不能同时调用 ...
- std::unordered_map与std::map
前者查找更快.后者自动排序,并可指定排序方式. 资料参考: https://blog.csdn.net/photon222/article/details/102947597
- 03.Jupyter Notebook高级-魔法命令
%run %run C:\Users\User\Desktop\hello.py hello world %timeit %timeit L = [i for i in range(1000)] 29 ...
- canal数据同步 客户端代码实现
1.引入相关依赖 <dependencies> <dependency> <groupId>org.springframework.boot</groupId ...
- Mysql通过binlog恢复误update的数据
事件: 在生产库执行update时只添加了STATUS(状态)条件,将所有状态为'E'的数据全部改为了'D' 思路: 操作步骤主要参考自文章:https://blog.csdn.net/weixin_ ...