【发布时间】:2014-08-16 18:02:10
【问题描述】:
我有一个包含以下数据的 xml 文件
<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>
我想使用 BeautifulSoup 返回仅 2013 年的 avgMpg 列表?我该怎么做?
我目前的努力是:
for item in soupedCarAvgMpgPage.findAll('year'):
listOfYears.append(''.join(item.findAll(text=True)))
for item in soupedCarAvgMpgPage.findAll('avgmpg'):
listOfAvgMpg.append(''.join(item.findAll(text=True)))
print listOfYears
print listOfAvgMpg;
dictionaryYearToAvgMpg = dict(zip(listOfYears, listOfAvgMpg));
但字典不接受重复:S
【问题讨论】:
标签: python python-2.7 beautifulsoup