Python调用WebService服务 查询天气预报

问题描述

Python调用Webservice服务,如WebXml的免费Web服务中的城市天气预报




安装

使用suds库的分支库suds-community,前者已在2010年停止更新

Suds是一款Python轻量级SOAP库

pip install suds-community




初试

调用MobileCodeWS Web 服务

from suds.client import Client

client = Client('http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl')  # 国内手机号码归属地查询
print(client)  # 获取服务的方法列表

# 具体方法查看http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx
print(client.service.getMobileCodeInfo("13000000000", ""))
Suds ( https://fedorahosted.org/suds/ )  version: 0.8.4

Service ( MobileCodeWS ) tns="http://WebXml.com.cn/"
   Prefixes (1)
      ns0 = "http://WebXml.com.cn/"
   Ports (2):
      (MobileCodeWSSoap)
         Methods (2):
            getDatabaseInfo()
            getMobileCodeInfo(xs:string mobileCode, xs:string userID)
         Types (1):
            ArrayOfString
      (MobileCodeWSSoap12)
         Methods (2):
            getDatabaseInfo()
            getMobileCodeInfo(xs:string mobileCode, xs:string userID)
         Types (1):
            ArrayOfString


13000000000:北京 北京 北京联通GSM卡




修复Schema

很多时候,WSDL定义的Schema或导入的Schema都失效了,最常见的问题是无法导入

Suds定义了一组用于修复Schema的模块——suds.xsd.doctor

调用天气预报服务

from suds.client import Client

client = Client('http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl')
print(client)

报错suds.TypeNotFound: Type not found: '(schema, http://www.w3.org/2001/XMLSchema, )'




from suds.client import Client
from suds.xsd.doctor import ImportDoctor, Import

imp = Import('http://www.w3.org/2001/XMLSchema', location='http://www.w3.org/2001/XMLSchema.xsd')
imp.filter.add('http://WebXml.com.cn/')
doctor = ImportDoctor(imp)
client = Client('http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl', doctor=doctor)
print(client)  # 获取服务的方法列表
# print(client.service.getSupportCityString(theRegionCode="广东"))  # 支持的城市列表
print(client.service.getWeather(theCityCode="2350"))  # 查询城市天气
Suds ( https://fedorahosted.org/suds/ )  version: 0.8.4

Service ( WeatherWS ) tns="http://WebXml.com.cn/"
   Prefixes (1)
      ns0 = "http://WebXml.com.cn/"
   Ports (2):
      (WeatherWSSoap)
         Methods (6):
            getRegionCountry()
            getRegionDataset()
            getRegionProvince()
            getSupportCityDataset(xs:string theRegionCode)
            getSupportCityString(xs:string theRegionCode)
            getWeather(xs:string theCityCode, xs:string theUserID)
         Types (91):
         ......


(ArrayOfString){
   string[] = 
      "广东 广州",
      "广州",
      "2350",
      "2020/06/10 10:14:52",
      "今日天气实况:气温:27℃;风向/风力:西南风 1级;湿度:95%",
      "紫外线强度:最弱。空气质量:较差。",
      "紫外线指数:最弱,辐射弱,涂擦SPF8-12防晒护肤品。
健臻·血糖指数:易波动,血糖易波动,注意监测。
穿衣指数:炎热,建议穿短衫、短裤等清凉夏季服装。
洗车指数:较适宜,无雨且风力较小,易保持清洁度。
空气污染指数:较差,气象条件较不利于空气污染物扩散。。
",
      "6月10日 阴",
      "25℃/33℃",
      "无持续风向小于3级",
      "2.gif",
      "2.gif",
      "6月11日 多云",
      "26℃/33℃",
      "无持续风向小于3级",
      "1.gif",
      "1.gif",
      "6月12日 多云转阴",
      "26℃/34℃",
      "无持续风向小于3级",
      "1.gif",
      "2.gif",
      "6月13日 多云转大雨",
      "25℃/34℃",
      "无持续风向小于3级转南风4-5级",
      "1.gif",
      "9.gif",
      "6月14日 大雨",
      "25℃/31℃",
      "南风转东南风4-5级",
      "9.gif",
      "9.gif",
 }




天气预报封装

接口设计参考高德地图天气查询API文档

项目地址:Weather

初始化对象,需要较长时间

>>> from weather import Weather
>>> weather = Weather(verbose=1)
Building supported regions and cities ...
Built succesfully. Cost 29.724 seconds.

支持的所有地区

>>> region = weather.getRegion()  # 支持的所有地区
>>> print('支持的地区数', len(region))
支持的地区数 90

支持的所有城市

>>> cities = weather.getCity()  # 支持的所有城市
>>> print('支持的城市数', len(cities))
支持的城市数 2562

地区支持的城市

>>> support = weather.getRegionCity(region='巴西')  # 支持的巴西的城市
>>> print('支持的巴西城市', support)
支持的巴西城市 ['里约热内卢', '圣保罗']

查询城市的天气

>>> weather.getWeather(city='神州')
{'city': '神州', 'status': 0}
>>>
>>> result = weather.getWeather(city='广州')  # 查询广州的天气
>>> for k,v in result.items():
...     print(k, v)
...
city 广东 广州
reporttime 2020/06/10 14:05:27
live 今日天气实况:气温:32℃;风向/风力:西南风 2级;湿度:70%
brief 紫外线强度:最弱。空气质量:较差。
index 紫外线指数:最弱,辐射弱,涂擦SPF8-12防晒护肤品。
健臻·血糖指数:易波动,血糖易波动,注意监测。
穿衣指数:炎热,建议穿短衫、短裤等清凉夏季服装。
洗车指数:较适宜,无雨且风力较小,易保持清洁度。
空气污染指数:较差,气象条件较不利于空气污染物扩散。。

today 6月10日 阴 25℃/33℃ 无持续风向小于3级
tomorrow 6月11日 多云 26℃/33℃ 无持续风向小于3级
day1 6月10日 阴 25℃/33℃ 无持续风向小于3级
day2 6月11日 多云 26℃/33℃ 无持续风向小于3级
day3 6月12日 多云转阴 26℃/34℃ 无持续风向小于3级
day4 6月13日 多云转大雨 25℃/34℃ 无持续风向小于3级转南风4-5级
day5 6月14日 大雨 25℃/31℃ 南风转东南风4-5级




参考文献

  1. WebXml | 免费Web服务
  2. suds-community · PyPI
  3. Python调用Webservice服务
  4. suds-community
  5. SOAP suds and the dreaded schema Type Not Found error
  6. Fixing Broken Schema
  7. 高德地图天气查询API文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XerCis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值