python xml解析会计缺少的标记

时间:2017-09-18 07:42:40

标签: xml python-3.x

我正在尝试解析来自https://www.boardgamegeek.com/xmlapi/collection/eekspider的xml数据;有些游戏缺少年度出版的标签(即自然小道游戏);我在代码中使用try / except处理了缺少的标记。我想知道是否有另一种方法可以做到这一点。

import xml.etree.ElementTree as ET
import urllib.request, urllib.parse, urllib.error
lista=[]
url=input('Please enter the url-')
xml= urllib.request.urlopen(url).read()
tree=ET.fromstring(xml)
lista=tree.findall('*')

for value in lista:
    try:

         print("Game name:",value.find('name').text)
         print("Publication Date:",value.find('yearpublished').text)
         #print("Statistics:", value.find('stats').attrib)
         print("----------")
         game=value.find('name').text
         counts[game]=counts.get(game,0)+1
         date=value.find('yearpublished').text
    except:
        pass
        print("Publication Date: unknown")
        print("----------")

1 个答案:

答案 0 :(得分:0)

由于文档说明了find()方法

  

返回元素实例或无

您还应该能够测试返回值是否为None,如下所示:

val = value.find('yearpublished')
if val is not None:
    date = val.text

find() here

的文档

这是实际产生的代码:

for value in lista:
    print("----------")
    print("Game name:",value.find('name').text)
    date = "unknown"
    game=value.find('name').text
    #counts[game]=counts.get(game,0)+1
    val = value.find('yearpublished')
    if val is not None:
        date = val.text
    print("Publication Date:",date)

示例输出:(跳过第一行..)

...
Game name: New World: A Carcassonne Game
Publication Date: 2008
----------
Game name: Nippon Rails
Publication Date: 1992
----------
Game name: Rat Hot
Publication Date: 2005
----------
Game name: Risk
Publication Date: 1959
----------
Game name: Russian Rails
Publication Date: 2004
----------
Game name: Skip-Bo
Publication Date: 1967
----------
Game name: Starship Catan
Publication Date: 2001
----------
Game name: Super Scrabble
Publication Date: 2004
----------
Game name: Ticket to Ride: Nordic Countries
Publication Date: 2007
----------
Game name: Times Square
Publication Date: 2006
----------
Game name: Upwords
Publication Date: 1981
----------
Game name: Xanth
Publication Date: 1991
----------
Game name: Zombie Fluxx
Publication Date: 2007