Есть ли простой способ конвертировать zeep-ответ в json, pandas, xml? - PullRequest
0 голосов
/ 25 июня 2019

Я использую Python 3.6 и Zeep 3.4.0

Zeep возвращает необработанные данные, и я не могу преобразовать их в объект xml / json / pandas.

Я пытался использовать bs4 дляполучить таблицу из текста1, не повезло.Сериализуйте text1, чтобы получить json, тоже не повезло.

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)

Мне нужно получить читаемую таблицу xml или json / pandas из zeep.

1 Ответ

0 голосов
/ 26 июня 2019

сам нашел способ:)

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