使用BeautifulSoup,如何防范未被发现的元素?

时间:2010-07-31 03:32:04

标签: python beautifulsoup

我循环遍历表中的表行,但前面的1行或2行没有我要查找的元素(它们用于表列标题等)。

所以说完第3个表格之后,表格单元格(td)中有一些元素可以满足我的要求。

e.g。

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

但是调用它会失败,因为前几行没有这个。

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

我收到的错误如下:

nonetype object is unsubscriptable

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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