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

查看:445
本文介绍了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的属性文本"时,就会发生异常.

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天全站免登陆