当前位置: 首页 > news >正文

(一)python发送HTTP 请求的两种方式(get和post )

引入 requests 包

import requests

注:发送请求(包括请求行、方法类型、头、体) & 常见的请求方式有get、post、put、delete

一、发送get请求

           格式:requests.get() (内容: url必填; params选填:url参数字典)

# ~ 无参数的get请求
res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince')
print(res.text)
#打印响应主体内容,字符串格式
# ~ 有参数的get请求
res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString?theRegionCode=3113')
print(res.text)
# ~ 使用params的get请求
res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString',params={"theRegionCode":3113})
print(res.text)

二、发送post请求


       知识扩展#  requests.post() post请求分为5种,常用的有三种,如下
      ①application/x-www-form-urlencod (form表单);
      ②raw (纯文本格式):有5种格式,分别为json/xml/Html/Text/JavaScrip
      ③multipart/form-data (复合式表单)

# ~ 无参数的post请求
# ~ 有正文体的post请求


1.   form表单(application/x-www-form-urlencod)        

-----data   后跟字典

eg.1: 带参数的查询接口

res = requests.post(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString',headers={"Content-Type": "application/x-www-form-urlencoded"},data={"theRegionCode": 3113})
print(res.text)


  eg.2: 带账号名密码的登录接口

res = requests.post(url='http://123.56.99.53:9000/event/api/admin/',headers={"Content-Type": "application/x-www-form-urlencoded"},data={"username": "admin", "password": "MTIzYWRtaW4="})
print(res.text)

2.   纯文本格式(raw)

 # ~ xml格式    ----data    后跟字符串

res = requests.post(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx',headers ={"Content-Type": "text/xml; charset=utf-8","SOAPAction": "http://WebXml.com.cn/getSupportCityString"},data = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><getSupportCityString xmlns="http://WebXml.com.cn/"><theRegionCode>string</theRegionCode></getSupportCityString></soap:Body>
</soap:Envelope>''')
print(res.text)

  # ~json格式       ----- json  后跟字典(常用), or ----data  后跟json字符串

res = requests.post(url='http://123.56.99.53:5000/event/weather/getWeather/',headers={"Content-Type": "application/json"},json={"theCityCode": "1001"})
print(res.text)

注:# json后面跟的是字典即json={"theCityCode": "1001"}),可改成 data=‘{"theCityCode": "1001"})’;字典能自动转为json字符串,所以一般用字典表示;建议纯json正文的接口使用第一种方式,除非是较长的字符串     

3.  复合式表单(multipart/form-data )  

  ------上传二进制文件    ---- key value (接口名称:文件名)

res = requests.post(url='http://123.56.99.53:9001/api/uploadFile/',headers={"Cookie": "uid=1;token=44c972f05d76fdd93c31f9c2b65bb098f308cdfc"#"Content-Type": "multipart/form-data"},
#有的接口不需要写正文体格式,老服务器一般需要写files={"myfile1": open('D:\全力以富\1.docx', 'rb')#"myfile2: open(上传多个文件)"})
print(res.text)

注:上传接口 files={‘文件路径’,‘rb'}  rb表示

相关文章:

  • C# Onnx DirectMHP 全范围角度2D多人头部姿势估计
  • ChatGPT如何管理对话历史?
  • 【Node.js入门】1.2 部署Node.js开发环境
  • 基于飞迪RTK/INS组合导航模组的里程计发布方法
  • centos7.9 postgresql 16.0 源码安装部署
  • MySQL进阶_5.逻辑架构和SQL执行流程
  • 数据结构(c语言版) 栈
  • 互联网系统安全(一)
  • 小程序员 scroll滚动与页面滚动冲突造成快速滑到底部卡顿失败问题
  • 蓝桥杯官网填空题(激光样式)
  • C#解析XML并反序列化为Model的方法
  • ubuntu20.04 安装cudnn
  • 单链表(3)
  • 成绩公布方式,这样操作更方便
  • 十三、W5100S/W5500+RP2040树莓派Pico<FTP Server>
  • ----------
  • create-react-app项目添加less配置
  • javascript数组去重/查找/插入/删除
  • React-生命周期杂记
  • Redash本地开发环境搭建
  • vue-loader 源码解析系列之 selector
  • 蓝海存储开关机注意事项总结
  • 批量截取pdf文件
  • 思考 CSS 架构
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • # Apache SeaTunnel 究竟是什么?
  • %@ page import=%的用法
  • (pytorch进阶之路)扩散概率模型
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (论文阅读40-45)图像描述1
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (五)Python 垃圾回收机制
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • ./configure、make、make install 命令
  • .NET 8.0 发布到 IIS
  • .net CHARTING图表控件下载地址
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例
  • .net 逐行读取大文本文件_如何使用 Java 灵活读取 Excel 内容 ?
  • .net开发引用程序集提示没有强名称的解决办法
  • .Net组件程序设计之线程、并发管理(一)
  • .sdf和.msp文件读取
  • /dev/sda2 is mounted; will not make a filesystem here!
  • @vue/cli脚手架
  • [20180312]进程管理其中的SQL Server进程占用内存远远大于SQL server内部统计出来的内存...
  • [2019.2.28]BZOJ4033 [HAOI2015]树上染色
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [8-27]正则表达式、扩展表达式以及相关实战
  • [android] 看博客学习hashCode()和equals()
  • [BZOJ]4817: [Sdoi2017]树点涂色
  • [C#小技巧]如何捕捉上升沿和下降沿
  • [C++]打开新世界的大门之C++入门
  • [C语言]编译和链接
  • [IE技巧] IE8中HTTP连接数目的变化
  • [IMX6DL] CPU频率调节模式以及降频方法