我正在使用BeautifulSoup解析HTML表,如下所示:
for tr in table_body.find_all('tr'):
for td in tr:
if td.text == 'Description':
description = td.find_next('td').text
if td.text == 'Category':
category = td.find_next('td').text
if td.text == 'Department':
department = td.find_next('td').text
if td.text == 'Justification':
justification = td.find_next('td').text
print(description, category, department, justification)
我将多个if语句重构为一个函数:
def html_check(td, text):
if td.text == text:
value = td.find_next('td').text
return value
像这样被称为:
for tr in table_body.find_all('tr'):
for td in tr:
description= html_check(td, 'Description')
category = html_check(td, 'Category')
department = html_check(td, 'Department')
justification = html_check(td, 'Justification')
print(description, category, department, justification)
我的问题是,当函数html_check找不到匹配项时,它将返回None,将被打印出来。这是不希望的。
有什么方法可以使此函数仅在满足if条件时才返回值?