python3 通过suds 调用webservice
2019-09-29 本文已影响0人
如果的if
背景
最近由于项目需要,在python3 中调用第三方服务webserice。本来也觉得是一件很简单的事情,直接百度搜索一些文章,直接demo一个 调用天气的webservice
免费的webservice网站,http://www.webxml.com.cn/zh_cn/web_services.aspx
demo代码
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)
# 使用字典传入参数
r =client.service.getSupportCityString(**{"theRegionCode": "四川"})
首先,给大家肯定其实代码没什么问题,可就是一直无法运行成功。
遇到问题的步骤
1. 方法一(成功)
安装suds
python 下,使用suds调用webserivice服务
pip install suds
开始报错
You are using pip version 9.0.3, however version 19.2.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
e:\py\test>pip install suds -i https://pypi.douban.com/simple
Collecting suds
Downloading https://pypi.doubanio.com/packages/bc/d6/960acce47ee6f096345fe5a7d9be7708135fd1d0713571836f073efc7393/suds-0.4.tar.gz (104kB)
100% |████████████████████████████████| 112kB 163kB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\huaxi2\AppData\Local\Temp\pip-build-fv8y9596\suds\setup.py", line 20, in <module>
import suds
File "C:\Users\huaxi2\AppData\Local\Temp\pip-build-fv8y9596\suds\suds\__init__.py", line 154, in <module>
import client
ModuleNotFoundError: No module named 'client'
----------------------------------------
mmp
说是要安装client好吧,我先装client
pip install client
# 然后装
pip install suds
运行时继续报错
发生异常: SyntaxError
invalid syntax (client.py, line 242)
File "E:\py\test\wstest.py", line 1, in <module>
from suds.client import Client
百度后,找到一个方法,安装suds-jurko
pip install suds-jurko
调试后,终于成功了
(ArrayOfString){
string[] =
"阿坝,1070",
"安县,1084",
"安岳,1542",
"巴塘,1097",
"巴中,1501",
"白玉,1063",
"宝兴,1103",
"北川,1087",
"苍溪,1495",
2. 方法二(失败)
百度上讲过suds目前没有维护了,建议安装suds-py3
pip install suds-py3
安装完成后,有些url不会报错,有些url就汇报如下错误,尝试过各种方式方法后,发现还是无法成功,建议各位就不用再试了。
File "D:\Program Files\py3\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "D:\Program Files\py3\lib\urllib\request.py", line 511, in open
req = Request(fullurl, data)
File "D:\Program Files\py3\lib\urllib\request.py", line 329, in __init__
self.full_url = url
File "D:\Program Files\py3\lib\urllib\request.py", line 355, in full_url
self._parse()
File "D:\Program Files\py3\lib\urllib\request.py", line 384, in _parse
raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: 'XMLSchema.dtd'
3.方法三(成功)
终于找到另外一篇文章,讲的直接安装suds-community
pip install suds_community
运行代码,完美~
(ArrayOfString){
string[] =
"阿坝,1070",
"安县,1084",
"安岳,1542",
"巴塘,1097",
"巴中,1501",
"白玉,1063",
"宝兴,1103",
"北川,1087",
"苍溪,1495",
引用
4. 方法四(理论上成功)
由于webservice是基于http和xml,所以可以直接使用requests模块进行模拟封装即可~
正所谓,“人生苦短,我用python”,推荐大家使用方法三