【发布时间】:2014-08-18 13:10:29
【问题描述】:
我有一个坐标如下的xml:
<geo>
<lat>52.5025100</lat>
<lng>13.3086000</lng>
</geo>
我可以像这样解析第一个和第二个孩子(存储在列表中)的字符串:
child_1=soup.find('geo').contents[1].get_text(strip=True)
child_2=soup.find('geo').contents[3].get_text(strip=True)
假设我必须处理多个文件,并且我不确定 lat 和 long 是否总是以上述顺序出现,索引将不起作用,因为它不可靠。而不是这个,我想用他们的标签名解析 lat 和 long 作为geo 的孩子。
这不起作用:
child_1=soup.find('geo').contents('lat').get_text(strip=True)
那么我该如何实现呢?
注意:lat 和 long 在文档中多次出现。因此我无法直接解析lat 和long 的文档
【问题讨论】:
标签: python beautifulsoup parent-child