为此following xml , 我如何获取 xml 然后解析它以获取 <age>
的值?
<boardgames>
<boardgame objectid="13">
<yearpublished>1995</yearpublished>
<minplayers>3</minplayers>
<maxplayers>4</maxplayers>
<playingtime>90</playingtime>
<age>10</age>
<name sortindex="1">Catan</name>
...
我目前正在尝试:
result = urlfetch.fetch(url=game_url)
xml = ElementTree.fromstring(result.content)
但我不确定自己是否走在正确的道路上。当我尝试解析时出现错误(我认为是因为 xml 不是有效的 xml)。
最佳答案
xml.findtext('age')
或 xml.findtext('boardgames/age')
通常会给你里面的 10 <age>10</age>
,但由于 xml 无效,解析似乎失败。 ElementTree
根据我的经验,解析无效 xml 的工作相当糟糕。
改为使用 BeautifulSoup ,可以很好地处理无效的 xml。
content = urllib2.urlopen('http://boardgamegeek.com/xmlapi/boardgame/13').read()
soup = BeautifulSoup(content)
print soup.find('age').string
关于python - 如何在 Google App Engine 上用 Python 解析 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4556924/