简体   繁体   中英

How to convert the xml file got using requests library to a dictionary

Before saying to use xmltodict ,i want to say that I can get the top layer but not all the layer. for example when i used ebay python sdk i used

 mydict =  d['searchResult']['item'][i]

this line of code to get the details of an item when i converted my xml to dict i got an ordered dict and i can only go a layer deep.

d = xmltodict.parse(r.text)

 >>> dict(d['FindProductsResponse']['Product'])
   Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ValueError: dictionary update sequence element #0 has length 8; 2 is required
 >>> 

I get this error when i try to go next level.the results are dict with in dict and I wonder may be xmltodict cant go that level or am i doing something wrong.

 import csv
 import sys
 import requests

 payload = {'callname':'FindProducts','responseencoding':'XML','siteid':'EBAY-IN',
        'appid':'MyAppID',
            'version':'967','QueryKeywords':'harry potter',
            'AvailableItemsOnly':'true',
            'MaxEntries':'2'}
 r = requests.get('http://open.api.ebay.com/shopping', params=payload)

 r.text

use BeautifulSoup , for example:

import bs4

soup = bs4.BeautifulSoup(r.text, 'lxml')
soup.findproductsresponse.errors

out:

<errors>
<shortmessage>Application ID invalid.</shortmessage>
<longmessage>Application ID invalid.</longmessage>
<errorcode>1.20</errorcode>
<severitycode>Error</severitycode>
<errorclassification>RequestError</errorclassification>
</errors>

Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM