使用BeautifulSoup,如何防止未找到元素?

1 投票
2 回答
2639 浏览
提问于 2025-04-16 02:06

我在一个表格中循环遍历每一行,但前面的一两行没有我需要的元素(那些是表格的列标题等)。

所以从第三行开始,表格单元格(td)里有我想要的内容。

比如:

td[0].a.img['src']

但是这样调用就会失败,因为前面的几行没有这些内容。

How can I guard against these cases so my script doesn't fail?

我会遇到这样的错误:

nonetype object is unsubscriptable

2 个回答

1

从tr开始:

for td in tr.findChildren('td'):
    img = td.findChild('img')
    if img:
        src = img.get('src', '')  # return a blank string if there's no src attribute
        if src:
            # do something with src
5

最简单明了的,如果你想让你的代码“在一行”:

theimage = td[0].a.img
if theimage is not None:
   use(theimage['src'])

或者,更好的是,把检查None的部分放到你自己写的小函数里,比如:

def getsrc(image):
  return None if image is None else image['src']

然后使用getsrc(td[0].a.img)

撰写回答