如何刷新BeautifulSoup的缓存?

时间:2014-07-30 07:29:07

标签: python html beautifulsoup

我使用BeautifulSoup修改html文件。但我认为我所做的修改不能立即回写给BeautifulSoup的缓存。例如:

>>> from bs4 import BeautifulSoup
>>> html = "<div>some content here</div>"
>>> soup = BeautifulSoup(html)
>>> soup.select("div")
[<div>some content here</div>]

>>> soup.select("div")[0]['class'] = 'test'
>>> soup.select("div")
[<div class="test">some content here</div>]

>>> soup.select(".test")
[]

正如您在此处所见,soup.select(".test")表示未找到任何内容。但是soup.select("div")显示确实存在一个标记,其中有一个名为test的类。

我推测缓存机制会导致此问题。谁能告诉我发生了什么?

1 个答案:

答案 0 :(得分:1)

缓存看起来不是问题。

soup.find("div", {"class":"test"})

返回<div class="test">some content here</div>