我有点问题。我正在使用Python解析一个大的xml文件。问题是xml文件是不可预测的,有时某些元素可能不在xml中,因此Python在查找时抛出异常。我希望Python忽略这个异常并继续寻找下一个元素。在
这是我目前的代码,但不起作用。如果找不到它要查找的元素,它将抛出一个异常并从try-except块中移出。在
# now we can parse the xml we fetched.
try:
user = {}
feedLinks = response.getElementsByTagName('gd:feedLink')
statistics = response.getElementsByTagName('yt:statistics')[0]
user['id'] = response.getElementsByTagName('id')[0].firstChild.data
user['channel_title'] = response.getElementsByTagName('title')[0].firstChild.data
user['profile_url'] = response.getElementsByTagName('link')[0].getAttribute('href')
user['author_name'] = response.getElementsByTagName('author')[0].firstChild.firstChild.data
user['author_uri'] = response.getElementsByTagName('uri')[0].firstChild.data
user['age'] = response.getElementsByTagName('yt:age')[0].firstChild.data
user['favourites_url'] = feedLinks[0].getAttribute('href')
user['contacts_url'] = feedLinks[1].getAttribute('href')
user['playlists'] = feedLinks[3].getAttribute('href')
user['subscriptions'] = feedLinks[4].getAttribute('href')
user['uploads'] = feedLinks[5].getAttribute('href')
user['new_subscription_videos'] = feedLinks[6].getAttribute('href')
user['statistics'] = {'last_access':statistics.getAttribute('lastWebAccess'),
'subscriber_count':statistics.getAttribute('subscriberCount'),
'video_watch_count':statistics.getAttribute('videoWatchCount'),
'view_count':statistics.getAttribute('viewCount'),
'total_upload_views':statistics.getAttribute('totalUploadViews')}
user['gender'] = response.getElementsByTagName('yt:gender')[0].firstChild.data
user['location'] = response.getElementsByTagName('yt:location')[0].firstChild.data
user['profile_pic_url'] = response.getElementsByTagName('media:thumbnail')[0].getAttribute('url')
user['username'] = response.getElementsByTagName('yt:username')[0].firstChild.data
except Exception, error:
# store the error for logging later
self.errors.append(str(error) + " from main.py:Crawler")
有人有什么想法吗?在
上面是将XML转换成python字典的函数。在
我采用的XML格式是:
^{pr2}$一旦你拿到字典,你的问题就减少了1000倍。在
在引发异常后,不能跳回try块。这是不可能的
但是,有两种方法可以让解析继续向前。第一种方法是将每个操作分解为自己的try块。回答不好。在
更好的方法是首先不要抛出异常。使用if语句检查数据是否存在且有效,而不是仅仅假设它存在。这样,您就可以完全控制每次文件格式不正确时发生的情况。在
我要做的是反复阅读字典(我真的很喜欢字典),做你以前做的每件事,但不同。如是(未经测试):
将
getAttribute
内容附加到数据中不需要太多修改。在相关问题 更多 >
编程相关推荐