最近用python测试使用webservice接口,发现根据号码查询归属地能够成功,而通过城市查询天气报错了,最终提示“suds.TypeNotFound: Type not found: ‘(schema, http://www.w3.org/2001/XMLSchema, )’”,解决后记录下来。

查看接口信息

from suds.client import Client
url = http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
print(Client(url))

如上代码,发现运行失败,报错如下图

发现需要给client方法doctor属性值,就不会报错了,于是就有了下面代码

from suds.client import Client
from suds.xsd.doctor import ImportDoctor,Import
url = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl'
imp = Import('http://www.w3.org/2001/XMLSchema',
                 location='http://www.w3.org/2001/XMLSchema.xsd')
imp.filter.add('http://WebXml.com.cn/')
print(Client(url,doctor = ImportDoctor(imp)))

运行时间变长了不少,但接口信息输出成功了,如下图

根据城市查询天气

如上图接口信息,发现getWeatherbyCityName方法是根据城市来查询天气的,那么就来试一下查询成都的天气吧!

from suds.client import Client
from suds.xsd.doctor import ImportDoctor,Import
url = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl'
imp = Import('http://www.w3.org/2001/XMLSchema',
                 location='http://www.w3.org/2001/XMLSchema.xsd')
imp.filter.add('http://WebXml.com.cn/')
c = Client(url,doctor = ImportDoctor(imp))
print(c.service.getWeatherbyCityName(u"成都"))

注意,该方法参数前必须加上u才行哦,成功如下,然后再根据自己所需再解析数据吧

本文地址:https://blog.csdn.net/qq_43400993/article/details/111880872