【发布时间】:2014-03-01 00:09:34
【问题描述】:
我正在尝试使用 beautifulsoap 读取 html 标签并检查某些标签是否可用或缺失。
我正在使用 beautifulsoup 读取文件,然后在我的测试文件中使用它。
这是我尝试过但没有成功的方法:
class Testing(unittest.TestCase):
@classmethod
def setUp(name):
name.html = None
with open("index.html") as frd:
name.html = frd.read()
name.soup = BeautifulSoup(name.html)
if not name.html:
raise Exception('cant read')
def testing(self)
assert self.soup.find('html') == 'html'
#Raise : error
我无法在汤中使用 find() 函数找到 html 标记(尝试打印它以查看输出,但 dint 工作)。如果 html 文件中缺少 HTML 标签,如何引发异常?
【问题讨论】:
-
您得到的确切错误是什么?我看到你没有 : 在定义之后!
-
断言 self.soup.find('html') == 'html' AssertionError
-
好的,你可以试试我刚刚发布的另一个答案吗?
-
显示更多代码。具体来说,你在哪里初始化
soup。 -
好的,试试这个答案,它工作正常!
标签: python beautifulsoup