I am parsing HTML table with BeautifulSoup like this:
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)
I refactored the multiple if
statements into a function:
def html_check(td, text):
if td.text == text:
value = td.find_next('td').text
return value
that is called like this:
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)
My problem is that when the function html_check
will not find a match, it will return None
, which will be printed. This is not desirable.
Is there any way to make this function return a value only when the if condition in it is met?