我正在使用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语句重构为一个函数:
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,将被打印出来。这是不希望的。
html_check
None
有什么方法可以使此函数仅在满足if条件时才返回值?
None如果在退出函数调用时未指定return ,Python将始终返回。您的选择是:
选项1(不满足条件时返回其他内容):
def html_check(td, text): if td.text == text: value = td.find_next('td').text return value return "no value found"
选项2(如果None返回则忽略该函数):
if html_check(td, 'section'): # do things