python - 如何在 Google App Engine 上用 Python 解析 xml

标签 python xml google-app-engine beautifulsoup elementtree

为此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/

相关文章:

python - 无效元素状态异常 : Message: invalid element state: Element is not currently interactable and may not be manipulated

Python Django 名称错误 : name 'datetime' is not defined

java - XML 到 java 对象的最佳 api

c# - 在 C# 和 C++ 之间交换对象

google-app-engine - Google APIs Explorer 没有从我的应用引擎应用程序中找到我可用的 ApiMethod

python - 如何检查opencv python中的cv2.circle是否位于cv2.rectangle内

python - 错误 : django. core.exceptions.AppRegistryNotReady : The translation infrastructure. .. 在 python 中

php - 我如何使 Xpath 搜索不区分大小写

google-app-engine - 具有自定义域的云存储上的 SSL

google-app-engine - Google App Engine 上的 Cron 作业语法