BeautifulSoup:如果未找到 HTML 元素,则返回 None [英] BeautifulSoup: Return None if HTML element not found

查看:42
本文介绍了BeautifulSoup:如果未找到 HTML 元素,则返回 None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 BeautifulSoup 来搜索网页中的多个元素.

I'm using BeautifulSoup to search for several elements in a web page.

我正在保存我找到的元素,但是因为我的脚本有可能会寻找一个元素并且它在解析的特定页面中不存在,所以我对每个元素都有 try/except 语句:

I'm saving the elements I find, but because there is a chance that my script will look for an element and it doesn't exist for the particular page it's parsing, I have try/except statements for every element:

# go through a bunch of webpages
for soup in soups:
    try: # look for HTML element
         data['val1'].append(soup.find('div', class_="something").text)
    except: # add NA if nothing found
        data['val1'].append("N/A")
    try:
        data['val2'].append(soup.find('span', class_="something else").text)
    except:
        data['val2'].append("N/A")

    # and more and more try/excepts for more elements of interest

有没有更简洁或更好的方式来写这样的东西?

Is there a cleaner or better way to write something like this?

推荐答案

根据关于 查找方法.如果找不到任何东西,它将返回 None .因此,当您调用 None 的属性文本"时会发生异常.

According to the documentation about the find method. It will return None if can’t find anything. So the Exception occurs when you call the property 'text' of None.

也许您应该看看 Python 中的 三元运算符 以了解你怎么做.

Maybe you should take a look at the ternary operator in Python to see how you can do it.

result = soup.find('div', class_="something")
data['val1'].append(result.text if result else "N/A")

正如 Dan-Dev 指出的那样捕捉异常很贵:

Also as Dan-Dev pointed out catching an exception is expensive:

如果没有引发异常,try/except 块非常有效.实际上捕获异常的代价很高.

A try/except block is extremely efficient if no exceptions are raised. Actually catching an exception is expensive.

这篇关于BeautifulSoup:如果未找到 HTML 元素,则返回 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆