我正在用这样漂亮的汤解析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条件满足时,才能使该函数返回值吗?