gpt4 book ai didi

python - 带有 Python 的 SOAP API

转载 作者:行者123 更新时间:2023-11-28 21:37:20 24 4
gpt4 key购买 nike

我尝试从砖 block 中提取一些信息。不幸的是,bricks API 是基于 SOAP 标准构建的,不支持 JSON。
如果我使用 HTTP 来获取数据,everythink 工作正常:

<ArrayOfSets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://brickset.com/api/">
<sets>
<setID>494</setID>
<number>1665</number>
<numberVariant>1</numberVariant>
<name>Dual FX Racers</name>

所以认证工作。我试图通过 Pythons (3.6) Urllib 得到响应:
import urllib.request

html = urllib.request.urlopen('https://brickset.com/api/v2.asmx/...')
print(html)

结果如下所示:

我已经尝试通过 beautifulsoup4 从这个 url 获取数据,但它没有用。每次我得到一个空数组。

编辑:
新代码取决于 1N5818 答案
from zeep import Client

wsdl_url = "https://brickset.com/api/?wsdl"
soap_client = Client(wsdl_url)
result = soap_client.getSet("xxx","xxx","494")

WSDL 独库:
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="https://brickset.com/api/">
<s:element name="getSet">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="apiKey" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="userHash" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="SetID" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>

好吧,我理解错了。但是,如果我执行此代码,则会收到以下错误:

AttributeError:“客户端”对象没有属性“getSet”

我做错了什么?

最佳答案

如果您要与 SOAP API 交互,您需要一个 SOAP 客户端来解析 WSDL url,否则您将无法创建一个客户端来像 RPC 一样调用。 Zeep是可用于 Python 的。你不能只用 CURL 或类似的东西与之交互。

您尝试与 API 交互的方式并不是 SOAP 的真正工作方式。 Soap 代表“简单对象访问协议(protocol)”。它的工作方式类似于 RPC(“远程程序调用”)。您需要创建一个对象,然后像静态类型的类对象一样调用它。为了创建该客户端(自动生成),您的代码需要在创建客户端之前知道该服务器的参数(它从 WSDL url“Web 服务描述语言”URL 获取)。

WSDL url 不是人类可读的(不是人类可读的),它们在那里为您将调用的对象提供有关 API 的足够信息。

您需要导入特定于 SOAP 的 Python 库。 Zeep例如

from zeep import Client

# Here's an example for calling data to a fake weather API.
data = {
"timestamp": "now",
"city": "London"
}
# It would have to know how to interact with the client
wsdl_url = "weather_service_url.com/?wsdl" # always ending in "wsdl" (web service description language"
# Now python knows what functions parameters are
# available and creates a client you can interact with.
soap_client = Client(wsdl_url)
# Then interact with the client like a class object
weather_api_result = soap_client.service.get_weather(**data)

编辑:

看起来根据您尝试使用的 API 的文档,几乎每个请求都需要一个 API_KEY。

这是他们的文档: https://brickset.com/tools/webservices/v2

这是使用他们的 API 的示例。我必须获得一个 API key ,然后通过电子邮件发送给我。
>>> from zeep import Client
>>> client = Client("https://brickset.com/api/v2.asmx?WSDL")
Forcing soap:address location to HTTPS
Forcing soap:address location to HTTPS
Forcing http:address location to HTTPS
Forcing http:address location to HTTPS
>>> result = client.service.checkKey("T2BZ-ODTf-LK5p"); # This was the API key sent to me, but you should get your own.
>>> result
'OK'

他们的网页 here显示 WSDL url 页面的链接(也在上面的代码中)。

根据 zeep 通知docs,您读入的每个 WSDL 文件都会创建一个 service对于客户端,因此对 API 的任何函数的每次调用都将在前面加上 client.service (准确地说是网络服务)。

它看起来像您尝试调用的方法 getSets需要您首先登录(或调用 login 方法)获取的一些信息。

您还可以通过为您的参数构造一个字典来调用 API,如下所示:
>>> data = {
... "apiKey": "T2BZ-ODTf-LK5p"
... }
>>> result = client.service.checkKey(**data);
>>> result
'OK'

您可以像这样调用登录表单:
>>> login_request_data = {
... "apiKey": "T2BZ-ODTf-LK5p",
... "username": "Whatever your username is",
... "passowrd": "Whatever your password is"
... }
>>> result = client.service.checkKey(**login_request_data);

关于python - 带有 Python 的 SOAP API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49702280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com