mike
mike

Reputation: 51

Is there an easy way to convert zeep response to json,pandas,xml?

I am using python 3.6 and zeep 3.4.0

Zeep returns raw data and i cannot convert it to xml/json/pandas object.

I've tried to use bs4 to get table from the text1, no luck. Serialize text1 to get json, no luck too.

from zeep import Client, Settings

settings = Settings(xml_huge_tree=True)

client = Client('http://www.cbr.ru/secinfo/secinfo.asmx?WSDL', settings=settings)
s = '2019-06-21T00:00:00'

with client.settings(raw_response=True):
    result = (client.service.IDRepoRUBXML(s))

#print(dir(result))    
text1 = (result.text)

print(text1)
#
#data = literal_eval(text1.decode('utf8'),)

def escape(t):
    """HTML-escape the text in `t`."""
    return (t.replace("&amp;","&").replace("&lt;","<" ).replace( "&gt;",">").replace("&#39;","'").replace("&quot;",'"'))

m = escape(text1)
print(m)

I need to retrieve readable xml or json/pandas table from zeep.

Upvotes: 5

Views: 9722

Answers (3)

David Muraya
David Muraya

Reputation: 161

If you're just trying to get a python dict type out of serialize_object helper, you can specify the type you want.

from zeep import helpers

_json = helpers.serialize_object(zeep_object, dict)

Upvotes: 16

h q
h q

Reputation: 1510

You can convert to XML using Minidom.

from zeep import Client
import xml.dom.minidom

client = Client('http://www.dneonline.com/calculator.asmx?wsdl')

def Add(num1, num2):
    with client.settings(raw_response=True):
        return xml.dom.minidom.parseString(client.service.Add(num1, num2).content).toprettyxml(indent="   ",encoding='utf8')

print (Add(2,5))

Upvotes: 0

mike
mike

Reputation: 51

found a way myself :)

from zeep import Client, Settings
from bs4 import BeautifulSoup

settings = Settings(xml_huge_tree=True)

client = Client('http://www.cbr.ru/secinfo/secinfo.asmx?WSDL', settings=settings)
s = '2019-06-21T00:00:00'

with client.settings(raw_response=True):
    result = (client.service.IDRepoRUBXML(s))

#print(dir(result))    
text1 = (result.text)

def escape(t):
    t = t.replace("&amp;","&")
    t1 = t.replace("&lt;","<" )
    t2 = t1.replace( "&gt;",">")
    t3 = t2.replace("&#39;","'")
    t4 = t3.replace("&quot;",'"')
    return t4

m = escape(text1)


#j = parser.feed(m)
if(m is not None):
    soup = BeautifulSoup(m,'lxml')
else:
     print("")

items = soup.find_all('item')

for item in items:
    discounts = item.find_all('dt')
    beg_6d = discounts[0]['beg']
    min_6d = discounts[0]['min']
    max_6d = discounts[0]['max']
    beg7_14 = discounts[1]['beg']
    min7_14 = discounts[1]['min']
    max7_14 = discounts[1]['max']         

    for attr in item.attrs:

        dateredemption = item.attrs['dateredemption']
        em = item.attrs['em']
        isin = item.attrs['isin']
        price = item.attrs['price_fnd']
        regn = item.attrs['regn']

    print(isin,regn,em,dateredemption,price,beg_6d,min_6d,max_6d, beg7_14,min7_14,max7_14) 

Upvotes: 0

Related Questions