简体   繁体   中英

issue with element tree not returning the correct values when parsing an xml

the program i'm having is the following. when parsing an xml file with et that has more than 2 childs, when i'm inside of the first child gathering data it works as intended, but when i'm inside the second it takes the data of the first. but when the xml has only 1 child it works as intended.

this is the xml file i'm parsing with only 1 child

<exec_api version="2.0" method="marketstat_xml">
 <marketstat>
  <type id="215">
   <buy>
    <volume>161730411</volume>
    <avg>1.91</avg>
    <stddev>2.09</stddev>
    <median>1.75</median>
    <percentile>2.86</percentile>
    <max>10.00</max>
    <min>1.00</min>
   </buy>
   <sell>
    <volume>62024574</volume>
    <avg>7.88</avg>
    <stddev>4.72</stddev>
    <median>6.90</median>
    <percentile>4.72</percentile>
    <max>29.00</max>
    <min>1.40</min>
   </sell>
  </type>
 </marketstat>
</exec_api>

this is the code i'm using atm

import urllib.request
from urllib.request import Request, urlopen
import xml.etree.ElementTree as ET
from decimal import Decimal


reqs = Request('https://api.evemarketer.com/ec/marketstat?typeid=215', headers={'User-Agent': 'Mozilla/5.0'})


with urllib.request.urlopen(reqs) as url1:
    tree = ET.parse(url1)
    root = tree.getroot()


for type_ in root.iter('type'):
    print(type_.attrib)

    for action in root.find('./marketstat/type'):
        print("  %s" % action.tag)

        if action.tag == 'buy':

            for buy in root.find("./marketstat/type/buy"):
                print("    %s:%.2f" % (  buy.tag   ,    Decimal(  buy.text.replace(',','.')  )  ))

        if action.tag == 'sell':

            for sell in root.find("./marketstat/type/sell"):
                print("    %s:%.2f" % (  sell.tag   ,    Decimal(  sell.text.replace(',','.')  )  ))

and this is the output of the program

{'id': '215'}
  buy
    volume:161730411.00
    avg:1.91
    stddev:2.09
    median:1.75
    percentile:2.86
    max:10.00
    min:1.00
  sell
    volume:62024574.00
    avg:7.88
    stddev:4.72
    median:6.90
    percentile:4.72
    max:29.00
    min:1.40

what the program is doing is searching on the type tag, then buy or sell, and returning the tags and values, then when i use this xml

reqs = Request('https://api.evemarketer.com/ec/marketstat?typeid=215,216', headers={'User-Agent': 'Mozilla/5.0'}) this is the only change needed to run the program with the new xml

<exec_api version="2.0" method="marketstat_xml">
 <marketstat>
  <type id="215">
   <buy>
    <volume>161730411</volume>
    <avg>1.91</avg>
    <stddev>2.09</stddev>
    <median>1.75</median>
    <percentile>2.86</percentile>
    <max>10.00</max>
    <min>1.00</min>
   </buy>
   <sell>
    <volume>62024574</volume>
    <avg>7.88</avg>
    <stddev>4.72</stddev>
    <median>6.90</median>
    <percentile>4.72</percentile>
    <max>29.00</max>
    <min>1.40</min>
   </sell>
  </type>
 <type id="216">
   <buy>
    <volume>154135069</volume>
    <avg>1.40</avg>
    <stddev>2.53</stddev>
    <median>1.00</median>
    <percentile>3.27</percentile>
    <max>8.47</max>
    <min>1.00</min>
   </buy>
   <sell>
    <volume>21843272</volume>
    <avg>12.17</avg>
    <stddev>20.15</stddev>
    <median>8.42</median>
    <percentile>4.71</percentile>
    <max>130.00</max>
    <min>0.17</min>
   </sell>
  </type>
 </marketstat>
</exec_api>

this is the result of the program

{'id': '215'}
  buy
    volume:161730411.00
    avg:1.91
    stddev:2.09
    median:1.75
    percentile:2.86
    max:10.00
    min:1.00
  sell
    volume:62024574.00
    avg:7.88
    stddev:4.72
    median:6.90
    percentile:4.72
    max:29.00
    min:1.40
{'id': '216'}
  buy
    volume:161730411.00
    avg:1.91
    stddev:2.09
    median:1.75
    percentile:2.86
    max:10.00
    min:1.00
  sell
    volume:62024574.00
    avg:7.88
    stddev:4.72
    median:6.90
    percentile:4.72
    max:29.00
    min:1.40

as you can see, the results of the transfer to, then as i increase the number of type id elements the same happens they all get the data from the first.

thanks, if you dont understand something about the code or question please let me know and ill try to awnser

import xml.etree.ElementTree as ET
from decimal import Decimal

xml = '''<exec_api version="2.0" method="marketstat_xml">
 <marketstat>
  <type id="215">
   <buy>
    <volume>161730411</volume>
    <avg>1.91</avg>
    <stddev>2.09</stddev>
    <median>1.75</median>
    <percentile>2.86</percentile>
    <max>10.00</max>
    <min>1.00</min>
   </buy>
   <sell>
    <volume>62024574</volume>
    <avg>7.88</avg>
    <stddev>4.72</stddev>
    <median>6.90</median>
    <percentile>4.72</percentile>
    <max>29.00</max>
    <min>1.40</min>
   </sell>
  </type>
 <type id="216">
   <buy>
    <volume>154135069</volume>
    <avg>1.40</avg>
    <stddev>2.53</stddev>
    <median>1.00</median>
    <percentile>3.27</percentile>
    <max>8.47</max>
    <min>1.00</min>
   </buy>
   <sell>
    <volume>21843272</volume>
    <avg>12.17</avg>
    <stddev>20.15</stddev>
    <median>8.42</median>
    <percentile>4.71</percentile>
    <max>130.00</max>
    <min>0.17</min>
   </sell>
  </type>
 </marketstat>
</exec_api>'''

root = ET.fromstring(xml)
for _type in root.findall('./marketstat/type'):
  print('type: {}'.format(_type.attrib.get('id')))
  buy = _type.find('./buy')
  for prop in list(buy):
    print('{} : {}'.format(prop.tag,prop.text))
  sell = _type.find('./sell')
  for prop in list(sell):
    print('{} : {}'.format(prop.tag,prop.text))
  print('')

output

type: 215
volume : 161730411
avg : 1.91
stddev : 2.09
median : 1.75
percentile : 2.86
max : 10.00
min : 1.00
volume : 62024574
avg : 7.88
stddev : 4.72
median : 6.90
percentile : 4.72
max : 29.00
min : 1.40

type: 216
volume : 154135069
avg : 1.40
stddev : 2.53
median : 1.00
percentile : 3.27
max : 8.47
min : 1.00
volume : 21843272
avg : 12.17
stddev : 20.15
median : 8.42
percentile : 4.71
max : 130.00
min : 0.17

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