Python忽略异常并从遇到异常的地方继续前进

2024-05-21 13:20:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有点问题。我正在使用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")

有人有什么想法吗?在


Tags: url元素dataresponsecountxmlauthorhref
3条回答
from lxml import etree
def parse():
                xmlFileName = '/home/shariq/abc2.xml'
                postsList = []
                tree = etree.parse(xmlFileName)
                for post in tree.xpath("//add/doc"):
                    thispost = {}
                    postxpath = tree.getpath(post)
                    for child in post:
                        fieldName = child.get("name").strip()
                        thispost[fieldName] = child.text
                    postsList.append(thispost)
                return postsList

上面是将XML转换成python字典的函数。在

我采用的XML格式是:

^{pr2}$

一旦你拿到字典,你的问题就减少了1000倍。在

在引发异常后,不能跳回try块。这是不可能的

但是,有两种方法可以让解析继续向前。第一种方法是将每个操作分解为自己的try块。回答不好。在

更好的方法是首先不要抛出异常。使用if语句检查数据是否存在且有效,而不是仅仅假设它存在。这样,您就可以完全控制每次文件格式不正确时发生的情况。在

我要做的是反复阅读字典(我真的很喜欢字典),做你以前做的每件事,但不同。如是(未经测试):

for key in dicto:
  try: user[key] = response.getElementsByTagName(dicto[key])
  except: print "mumble mumble"; continue

getAttribute内容附加到数据中不需要太多修改。在

相关问题 更多 >