python - BeautifulSoup: How to get the nearest tag -
i have xml file following data
<year>2013</year> <yousavespend>2500</yousavespend> <yourmpgvehicle> <avgmpg>32.261695541</avgmpg> <citypercent>43</citypercent> <highwaypercent>57</highwaypercent> </yourmpgvehicle> <year>2013</year> <yousavespend>3000</yousavespend> <yourmpgvehicle> <avgmpg>33.383275416</avgmpg> <citypercent>49</citypercent> <highwaypercent>51</highwaypercent> </yourmpgvehicle> <year>2012</year> <yousavespend>2500</yousavespend> <yourmpgvehicle> <avgmpg>36.210640188</avgmpg> <citypercent>32</citypercent> <highwaypercent>68</highwaypercent> </yourmpgvehicle>
i want use beautifulsoup return list of avgmpg year 2013? how can that?
my current effort has been:
for item in soupedcaravgmpgpage.findall('year'): listofyears.append(''.join(item.findall(text=true))) item in soupedcaravgmpgpage.findall('avgmpg'): listofavgmpg.append(''.join(item.findall(text=true))) print listofyears print listofavgmpg; dictionaryyeartoavgmpg = dict(zip(listofyears, listofavgmpg));
but dictionary not accept duplicates :s
since know elements going near each other, can there searching through next_siblings
:
from bs4 import beautifulsoup open('mpg.xml') f: contents=f.read() mpgs = beautifulsoup(contents) def find_nearest_vehicle(elem): sibling in elem.next_siblings: if sibling.name == 'yourmpgvehicle': return sibling def find_avg_mpg(elem): child in elem.children: if child.name == 'avgmpg': return child year_2013 = [year year in mpgs.find_all('year') if year.string == '2013'] avgmpg = [find_avg_mpg(find_nearest_vehicle(elem)).string elem in year_2013] print(avgmpg)
when run on file, get:
$ python3 mpg.py ['32.261695541', '33.383275416']
Comments
Post a Comment