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 ...
随机推荐
- JVM终结篇
1.1 重新认知JVM 之前我们画过一张图,是从Class文件到类装载器,再到运行时数据区的过程.现在咱们把这张图不妨丰富完善一下,展示了JVM的大体物理结构图. 1.2 GC优化 内存被使用了之后, ...
- input composition event All In One
input composition event All In One input event compositionStart & compositionEnd & compositi ...
- Flutter Widgets
Flutter Widgets Flutter 组件 Syncfusion Flutter Widgets 所有组件均支持即装即用的 Android,iOS和 Web not free https:/ ...
- git config [section] solutions
git config [section] solutions fix git [section] warnings global config $ vim ~/.gitconfig [user] em ...
- Gradle & Java
Gradle & Java Gradle Build Tool I Modern Open Source Build Automation https://gradle.org/ https: ...
- git & github & git clone & 'git clone' failed with status 128
git & github & git clone & 'git clone' failed with status 128 'git clone' failed with st ...
- Flutter framework & Flutter basics
Flutter framework & Flutter basics https://flutter.dev/docs/get-started/learn-more UI widgets ht ...
- OSS & Object Storage Service
OSS & Object Storage Service Object Storage Service https://en.wikipedia.org/wiki/Object_storage ...
- EventBus / Event Bus
EventBus / Event Bus EventEmitter / Event Emitter https://greenrobot.org/eventbus/documentation/ htt ...
- Dart: 请求graphql数据
import 'package:http/http.dart' as http; const url = "http://127.0.0.1:4000/graphql"; main ...