使用BeautifulSoup,如何防范元素没有被发现? [英] Using BeautifulSoup, how to guard against elements not being found?

查看:137
本文介绍了使用BeautifulSoup,如何防范元素没有被发现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过表中的行表中的循环,但第1或2行没有我要找的元素(它们是表的列标题等)。

所以说,后第3表行,也有说有什么我寻找的表格单元格(TD)元素。

例如

  TD [0] .a.img ['src'中]

不过,打电话,因为前几行失败没有这个。

 如何防范这些情况使我的脚本不失败?

我得到这样的错误:

  nonetype对象unsubscriptable


解决方案

最简单和最清晰的,如果你希望你的code看齐:

  theimage = TD [0] .a.img
如果theimage不是无:
   使用(theimage ['SRC'])

或者,preferably,敷检查自己的一个小功能,例如:

 高清getsrc(图片):
  返回None,如果图像是无别的图像['src'中]

和使用 getsrc(TD [0] .a.img)

I am looping through table rows in a table, but the first 1 or 2 rows doesn't have the elements I am looking for (they are for table column headers etc.).

So after say the 3rd table row, there are elements in the table cells (td) that have what I am looking for.

e.g.

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

But calling this fails since the first few rows don't have this.

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

I get errors like:

nonetype object is unsubscriptable

解决方案

Simplest and clearest, if you want your code "in line":

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

Or, preferably, wrap the None check in a tiny function of your own, e.g.:

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

and use getsrc(td[0].a.img).

这篇关于使用BeautifulSoup,如何防范元素没有被发现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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