当前位置: 首页 > 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>
  • 3.7、@ResponseBody 和 @RestController
  • Angular4 模板式表单用法以及验证
  • Javascript设计模式学习之Observer(观察者)模式
  • Java程序员幽默爆笑锦集
  • JS笔记四:作用域、变量(函数)提升
  • markdown编辑器简评
  • Octave 入门
  • springboot_database项目介绍
  • Sublime text 3 3103 注册码
  • Twitter赢在开放,三年创造奇迹
  • 类orAPI - 收藏集 - 掘金
  • 七牛云假注销小指南
  • 前嗅ForeSpider中数据浏览界面介绍
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 阿里云移动端播放器高级功能介绍
  • # 透过事物看本质的能力怎么培养?
  • #include
  • #QT(串口助手-界面)
  • (a /b)*c的值
  • (C++20) consteval立即函数
  • (Python第六天)文件处理
  • (Ruby)Ubuntu12.04安装Rails环境
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (三)Honghu Cloud云架构一定时调度平台
  • (未解决)macOS matplotlib 中文是方框
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • (一)appium-desktop定位元素原理
  • (一)Linux+Windows下安装ffmpeg
  • (转)c++ std::pair 与 std::make
  • (转)关于多人操作数据的处理策略
  • *p++,*(p++),*++p,(*p)++区别?
  • .“空心村”成因分析及解决对策122344
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .NET Core 和 .NET Framework 中的 MEF2
  • .NET/C# 在 64 位进程中读取 32 位进程重定向后的注册表
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境
  • .NET企业级应用架构设计系列之开场白
  • [ 转载 ] SharePoint 资料
  • [].shift.call( arguments ) 和 [].slice.call( arguments )