如何防止Python函数返回None

发布于 2021-01-29 14:59:45

我正在使用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条件时才返回值?

关注者
0
被浏览
74
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    None如果在退出函数调用时未指定return ,Python将始终返回。您的选择是:

    • 如果不满足条件,则返回其他内容。
    • 如果返回则忽略该函数 None

    选项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
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看