假设我有一些(简化的)BeautifulSoup代码,将数据拉入字典:
tournament_info = soup.find_all('li')
stats['Date'] = tournament_info[0].text
stats['Location'] = tournament_info[1].text
stats['Prize'] = tournament_info[3].text.split(':')[1].strip()
在初始find_all返回异常的情况下,我希望所有字典条目都是'无'。如果任何单个字典分配返回异常,我想要“无”#39;无太
有没有什么好方法可以写这个,除了下面那些可怕的东西?
try:
tournament_info = soup.find_all('li')
except:
m_stats['Date'] = 'None'
m_stats['Location'] = 'None'
m_stats['Prize'] = 'None'
try:
m_stats['Date'] = tournament_info[0].text
except:
m_stats['Date'] = 'None'
try:
m_stats['Location'] = tournament_info[1].text
except:
m_stats['Location'] = 'None'
try:
m_stats['Prize'] = tournament_info[3].text.split(':')[1].strip()
except:
m_stats['Prize'] = 'None'
答案 0 :(得分:0)
以下是我可以为您的代码建议的内容:
info = soup.find_all('li')
if not info:
m_stats = dict.fromkeys(m_stats, None)
return
mappings = {
'Date': 0,
'Location': 1,
'Prize': 3
}
for key in mappings:
value = None
try:
value = info[mappings[key]].text
if mappings[key] == 3:
value = value.split(':')[1].strip()
except IndexError:
pass
m_stats[key] = value
或者,您可以创建一个能够处理异常的函数:
def get_value(idx):
value = None
try:
value = info[idx].text
except IndexError:
pass
return value
m_stats['Date'] = get_value(0)
m_stats['Location'] = get_value(1)
m_stats['Prize'] = get_value(3)
if m_stats['Prize']:
m_stats['Prize'].split(':')[1].strip()
答案 1 :(得分:0)
创建自己的类
class Stats(dict):
tournament_info = []
def __init__(self, tournament_info, **kwargs):
super(Stats, self).__init__(**kwargs)
self.tournament_info = tournament_info
self['Date'] = self.get_tournament_info_text(0)
self['Location'] = self.get_tournament_info_text(1)
prize = self.get_tournament_info_text(2)
if prize is not None:
prize = prize.split(':')[1].strip()
self['Prize'] = prize
def get_tournament_info_text(self, index):
try:
return self.tournament_info[index]['text']
except:
return None
tournament_info = [
{
'text': 'aaa'
},
{},
{
'text': 'bbb:ccc '
}
]
m_stats = Stats(tournament_info)
print m_stats
答案 2 :(得分:0)
我选择的解决方案是创建一个空白模板字典(实际上是一个JSON),所有键都设置为“无”。
每次抓取页面时,首先使用此空白字典(从JSON加载)初始化m_stats。如果发生异常,则只是简单地传递(带有一些日志记录),并将值保留为“None”。因此,无需每次都明确指定“无”。
不确定将此标记为“答案”是否正确,因为它非常符合我的需要,但这就是我所做的。