首页 > 编程语言 >Python的GET和POST请求

Python的GET和POST请求

时间:2022-11-23 18:22:06浏览次数:40  
标签:请求 GET Python res url json POST data requests

引入 requests 包

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

一、发送get请求

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

  • 无参数的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: 带参数的查询接口
url="https://api.wl1688.net/iotc/getway"   #请求连接
headers={
"Content-Type": "application/json;charset=UTF-8"
}  #请求头,默认基本上是这个
data={
"currentPage":1,
"pageSize":1000
} #根据实际的填写
try:
   r = requests.post(url,headers=headers, data=json.dumps(data))
   print(r)
except requests.exceptions.RequestException as e:
   print('请求失败')

 eg.2: 带账号名密码的登录接口
url="https://api.wl1688.net/iotc/getway"   #请求连接
headers={
"Content-Type": "application/json;charset=UTF-8"
}  #请求头,默认基本上是这个
body={
"currentPage":1,
"pageSize":1000
} #根据实际的填写
try:
   r = requests.post(url,headers=headers, data=json.dumps(body))
   print(r)
except requests.exceptions.RequestException as e:
   print('请求失败')
  1. 纯文本格式(raw)
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)

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

~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)

标签:请求,GET,Python,res,url,json,POST,data,requests
From: https://www.cnblogs.com/jubai/p/16919381.html

相关文章

  • 6. python和Redis交互
    1安装包pipinstallredis2引入模块fromredisimport*这个模块中提供了StrictRedis对象(Strict严格),用于连接redis服务器,并按照不同类型提供了不同方法,进行交互操作......
  • Python 性能检测分析方法 - 时间、空间衡量方法
    性能检测分析方法-时间、空间衡量方法Python自带模块importtime点击查看代码#仅仅是示范time模块的用法,此段不能直接运行,运行请看测试项目源码importtime......
  • python四十期--mysql
    昨日内容回顾存取数据演变史1.文本文件2.目录规范3.数据库ps:数据处理统一数据库应用史1.本地存储2.网络存储ps:数据库集群数据库本质数据库在不同的场......
  • python爬取房产信息(仅供学习使用)
    importrequestsfrombs4importBeautifulSoupimporttimeimportopenpyxldefwrite_mysql(lst):wk=openpyxl.Workbook()sheet=wk.activeforiinlst:......
  • [VS] .nuget文件夹迁移
    记录下之前遇到.nuget文件夹占用C盘的问题。1.找到NuGet.config-- C:\ProgramFiles(x86)\NuGet\Config2.把原来的.nuget文件夹移到新路径。可以使用命令 dotnet......
  • python+OpenCv 图像噪声(椒盐噪声、高斯噪声)
    由于图像采集、处理、传输等过程不可避免的会受到噪声的污染,妨碍人们对图像理解及分析处理。常见的图像噪声有高斯噪声、椒盐噪声等。一、椒盐噪声椒盐噪声也称为脉冲......
  • python——定时执行,间隔5s执行一次,blockingScheduler
    本来一开始使用的time.sleep(5),但是执行时间长了之后,会与实际时间有偏差,因为没有计算程序本身运行的时间。 解决:使用blockingScheduler  将send()这个方法,每5s执......
  • python 使用 requests,requests_toolbelt上传图片文件,报错:AttributeError: 'int' objec
    问题在使用pythonrequests_toolbelt库上传excel文件的过程中,几类问题报错1MultipartEncoder支持中文文件名称上传开始遇到的报错以为是中文文件名称不支持,查到的......
  • python中的集合
    集合,简称集。由任意个元素构成的集体。高级语言都实现了这个非常重要的数据结构类型。Python中,它是可变的、无序的、不重复的元素的集合set()->newemptysetobjects......
  • python中利用pipreqs查询并安装项目所依赖的所有包
    pipreqs的安装pipinstallpipreqs-ihttp://pypi.douban.com/simple--trusted-hostpypi.douban.com在terminal中,如要查看test文件夹下脚本所依赖包,则输入如下命令:pi......