在 Python 中执行延迟执行并获取异常默认值
Do lazy execution and get default value on exception in Python
我正在解析 HTML 并且文件中有很多可选属性,如果在我读取它们时出现异常,我会改用一些默认值。
有什么方法可以准备一个通用函数来尝试检索 属性 并在异常 return 时检索默认值?
目前,我有这样的东西,但是非常难看。
try:
title = soup.find('h1').text
except:
title = "b/d"
try:
location = soup.find('a', attrs={'href': '#map'}).text
except:
location = "none"
try:
downside= soup.find('strong', attrs={'aria-label': 'downside'}).text
except:
downside = "0"
try:
incremental = soup.find('div', attrs={'aria-label': 'incremental'}).contents[3].text
except:
incremental = "1"
try:
difference = soup.find('div', attrs={'aria-label': 'difference'}).contents[1].text
except:
difference = "2"
try:
part = soup.find('div', attrs={'aria-label': 'part'}).contents[1].text
except:
part = "3"
- 不要捕获裸异常。
实现泛型函数的一种直接方法是
def get_attribute_text(soup, element, attrs, default_value, contents_index=None):
try:
if contents_index:
return soup.find(element, attrs=attrs).contents[contents_index].text
return soup.find(element, attrs=attrs).text
except AttributeError:
return default_value
并像这样使用:
title = get_attribute_text(soup, 'h1', {}, 'b/d')
location = get_attribute_text(soup, 'a', {'href': '#map'}, 'none')
...
incremental = get_attribute_text(soup, 'div', {'aria-label': 'incremental'}, '1', 3)
...
我正在解析 HTML 并且文件中有很多可选属性,如果在我读取它们时出现异常,我会改用一些默认值。 有什么方法可以准备一个通用函数来尝试检索 属性 并在异常 return 时检索默认值? 目前,我有这样的东西,但是非常难看。
try:
title = soup.find('h1').text
except:
title = "b/d"
try:
location = soup.find('a', attrs={'href': '#map'}).text
except:
location = "none"
try:
downside= soup.find('strong', attrs={'aria-label': 'downside'}).text
except:
downside = "0"
try:
incremental = soup.find('div', attrs={'aria-label': 'incremental'}).contents[3].text
except:
incremental = "1"
try:
difference = soup.find('div', attrs={'aria-label': 'difference'}).contents[1].text
except:
difference = "2"
try:
part = soup.find('div', attrs={'aria-label': 'part'}).contents[1].text
except:
part = "3"
- 不要捕获裸异常。
实现泛型函数的一种直接方法是
def get_attribute_text(soup, element, attrs, default_value, contents_index=None):
try:
if contents_index:
return soup.find(element, attrs=attrs).contents[contents_index].text
return soup.find(element, attrs=attrs).text
except AttributeError:
return default_value
并像这样使用:
title = get_attribute_text(soup, 'h1', {}, 'b/d')
location = get_attribute_text(soup, 'a', {'href': '#map'}, 'none')
...
incremental = get_attribute_text(soup, 'div', {'aria-label': 'incremental'}, '1', 3)
...