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 ...
随机推荐
- certbot 获取数字证书失效问题
title: certbot 获取数字证书失效问题 author: Narule date: 2021-02-18 10:45:00 +0800 categories: [Technology^技术, ...
- css variables & CSS 变量
css variables & CSS 变量 https://gist.github.com/xgqfrms-GitHub/5d022a13292c615d2730e84d909e1aba c ...
- Emmet & VSCode
Emmet & VSCode Emmet - the essential toolkit for web-developers https://emmet.io/ https://emmet. ...
- Python Learning Paths
Python Learning Paths Python Expert Python in Action Syntax Python objects Scalar types Operators St ...
- base 64 bug & encodeURIComponent
base64 bug & encodeURIComponent window.btoa("jëh²H¶�%28"); // "autoskiptoclMjiu&q ...
- vue & vue router & match bug
vue & vue router & match bug match bugs solution name must be router https://stackoverflow.c ...
- 2021,狗狗币暴涨,VAST更加疯狂!
2021年的币圈,多多少少出乎人的意料.很多币圈玩家们看好比特币,比特币却在达到4万美金的巅峰之后,极速下滑.很多玩家们看好以太坊,以太坊的手续费却出现了飙涨,让众多的中小散户们再次退却.而前几年已经 ...
- 注解处理器APT详解
本文转载自ANNOTATION PROCESSING 101 Introduction In this blog entry I would like to explain how to write ...
- ElasticSearch 中的 Mapping
公号:码农充电站pro 主页:https://codeshellme.github.io 1,ES 中的 Mapping ES 中的 Mapping 相当于传统数据库中的表定义,它有以下作用: 定义索 ...
- Dubbo与Zookeeper开发
1.Dubbo 1.1RPC RPC全称是remote procedure call,即远程过程调用.比如有两台服务器A和B,它们上面分别部署了一个服务.此时B服务器想调用A服务器上提供的方法,由于不 ...