问题描述
Python调用Webservice服务,如WebXml的免费Web服务中的城市天气预报
安装
使用suds
库的分支库suds-community
,前者已在2010年停止更新
Suds
是一款Python轻量级SOAP库
pip install suds-community
初试
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级