添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
苦闷的奔马  ·  Java-IO ...·  1 年前    · 
冷冷的春卷  ·  SQL ...·  1 年前    · 
卖萌的眼镜  ·  ChangeServiceConfig2设置 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I try to get extract some informations from bricks. Unfortunately bricks API is build on SOAP standart and don´t support JSON. If I use HTTP for getting the data everythink works fine:

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

So the authentification works. I tried to get the response by Pythons (3.6) Urllib:

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

The result looks like that:

I already tried to get the data from this url by beautifulsoup4 but it doesnt worked. I got everytime an empty array.

EDIT: new code depending on 1N5818 answer

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

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>

Ok I got my misunderstanding. But if I do this code I get following Error:

AttributeError: 'Client' object has no attribute 'getSet'

What did I wrong?

The way soap works is you need to create a link via an WSDL file and then use it like an RPC. – Alexander Kleinhans Apr 7, 2018 at 1:24

If you're going to interact witha SOAP API you need a SOAP client to parse the WSDL url or else you won't be able to create a client to call like an RPC. Zeep is one that's available for Python. You can't just interact with it with CURL or something like that.

How you're trying to interact with the API is not really the way SOAP works. Soap stands for "Simple Object Access Protocol". It works like an RPC ("Remote Procedural Call"). You need to create an object, then call it like it was a statically typed class object. In order to create that client (auto-generated), you code needs to know the parameters of that server before a client is created (which it gets from the WSDL url "Web Service Description Language" URL).

WSDL urls are not human-readable (not intended to be human readable), they're there to give just enough information about the API for the object you'll be calling from.

You need to import a SOAP specific Python library. Zeep for example

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)

EDIT:

It looks like per the documentation for the API you're trying to use, an API_KEY is required for almost every request.

Here's their documentation: https://brickset.com/tools/webservices/v2

Here's an example of using their API. I had to obtain an API key and it was emailed to me.

>>> 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

Their webpage here shows the link the the WSDL url page (which is also in the above code).

Notice per the zeep docs, every WSDL file you read in will create a service for the client, so every call to any function of the API will be prepended with client.service (web-service to be precise).

It looks like the method you're trying to call getSets requires some information that you need to get by logging in first (or by calling the login method).

You could also call the API by constructing a dictionary for your parameters like this:

>>> data = {
... "apiKey": "T2BZ-ODTf-LK5p" 
... }
>>> result = client.service.checkKey(**data);
>>> result

You would call the login form like this:

>>> login_request_data = {
... "apiKey": "T2BZ-ODTf-LK5p",
... "username": "Whatever your username is",
... "passowrd": "Whatever your password is"
... }
>>> result = client.service.checkKey(**login_request_data);
                Thank you a lot for your support. AWESOME! It works and i understand how soap works. I spend hours! What a waste. Thank you a lot again.
– Nico Schuck
                Apr 7, 2018 at 10:04
                Thank you, this was helpful and I managed to get it working. The first code example with a fake website was a bit confusing, but this one in the official docs served in place of it.
– user10417531
                May 7, 2019 at 15:54
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.