我正在查找html文档中的所有文本,我想添加span标记,它包含关于每个文本的一些信息,如下所示def recursiveChildren(x):
if "childGenerator" in dir(x):
for child in x.childGenerator():
recursiveChildren(child)
else:
if not x.isspace():
content = x.string
new_tag = soup.new_tag("span")
content.insert_before(new_tag)
if __name__ == "__main__":
with open("path to html",'r') as file:
soup = BeautifulSoup(file)
for child in soup.body.childGenerator():
recursiveChildren(child)
我得到错误,因为字符串对象之前没有属性插入。如何在字符串之前插入标记。我还尝试获取字符串的父标记,并在父标记内容之前添加add span标记,但如果有嵌套标记,则会产生问题,因为我希望在每个独立字符串之前都有一个span标记。有可能吗?类似于下面的但是在Python中Insert span tag before # content text
Html示例:
^{pr2}$
我想要的输出
****This is first string **** Second String **** ,