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?
return ""at the end of your function, outside of theifcondition. If you don't return anything from your function explicitly,Nonewill be returned.Nonereturned from a function isn't automatically printed. Just use anotherifcheck to see if the function returnsNoneand onlyprint()when it returns something other thanNone.Noneif you don't specify a return value for the given input.NoneNone, but you can't prevent the interpreter from returning it. Just call the function conditionally.