我试图从砖块中提取一些信息。不幸的是,Bricks API是基于SOAP标准构建的,并且不支持JSON。如果我使用HTTP获取数据,那么一切都可以正常工作:
<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>
Run Code Online (Sandbox Code Playgroud)
因此身份验证有效。我试图通过Python(3.6)Urllib来获得响应:
import urllib.request
html = urllib.request.urlopen('https://brickset.com/api/v2.asmx/...')
print(html)
Run Code Online (Sandbox Code Playgroud)
结果看起来像这样:
我已经尝试通过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")
Run Code Online (Sandbox Code Playgroud)
WSDL DOKU:
<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>
Run Code Online (Sandbox Code Playgroud)
好吧,我误会了。但是,如果我执行此代码,则会出现以下错误:
AttributeError:“客户端”对象没有属性“ getSet”
我怎么了
如果要与SOAP API进行交互,则需要SOAP客户端来解析WSDL URL,否则将无法创建像RPC一样进行调用的客户端。Zeep是可用于Python的一种。您不能只通过CURL或类似的东西与之交互。
您尝试与API交互的方式实际上并不是SOAP的工作方式。肥皂代表“简单对象访问协议”。它的工作方式类似于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)
Run Code Online (Sandbox Code Playgroud)
编辑:
看起来像您要使用的API的文档一样,几乎每个请求都需要一个API_KEY。
这是他们的文档:https : //brickset.com/tools/webservices/v2
这是使用其API的示例。我必须获取一个API密钥,然后将其通过电子邮件发送给我。
>>> 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'
Run Code Online (Sandbox Code Playgroud)
他们的网页在这里显示的链接的WSDL URL页面(这也是在上面的代码)。
请注意,根据zeep文档,您读入的每个WSDL文件都将为service客户端创建一个,因此对API的任何功能的每次调用都将带有前缀client.service(确切地说是Web服务)。
您尝试调用的方法似乎getSets需要先登录(或通过调用该login方法)获取一些信息。
您还可以通过为参数构造一个字典来调用API,如下所示:
>>> data = {
... "apiKey": "T2BZ-ODTf-LK5p"
... }
>>> result = client.service.checkKey(**data);
>>> result
'OK'
Run Code Online (Sandbox Code Playgroud)
您将这样调用登录表单:
>>> login_request_data = {
... "apiKey": "T2BZ-ODTf-LK5p",
... "username": "Whatever your username is",
... "passowrd": "Whatever your password is"
... }
>>> result = client.service.checkKey(**login_request_data);
Run Code Online (Sandbox Code Playgroud)