我找到了要替换的文本,但是当我打印soup
时,格式发生了变化。<div id="content">stuff here</div>
变得<div id="content">stuff here</div>
.如何保留数据?我已经尝试过print(soup.encode(formatter="none"))
,但这会产生相同的错误格式。
from bs4 import BeautifulSoup
with open(index_file) as fp:
soup = BeautifulSoup(fp,"html.parser")
found = soup.find("div", {"id": "content"})
found.replace_with(data)
当我打印found
时,我得到正确的格式:
>>> print(found)
<div id="content">stuff</div>
index_file
内容如下:
<!DOCTYPE html>
<head>
Apples
</head>
<body>
<div id="page">
This is the Id of the page
<div id="main">
<div id="content">
stuff here
</div>
</div>
footer should go here
</div>
</body>
</html>
found
对象不是 Python 字符串,它是一个恰好有一个很好的字符串表示形式的Tag
。您可以通过执行以下操作来验证这一点
type(found)
Tag
是 Beautiful Soup 创建的对象层次结构的一部分,以便您能够与 HTML 进行交互。另一个这样的对象是NavigableString
.NavigableString
很像一个字符串,但它只能包含进入HTML内容部分的内容。
当你这样做时
found.replace_with('<div id="content">stuff here</div>')
您要求将Tag
替换为包含该文字文本的NavigableString
。HTML能够显示该字符串的唯一方法是转义所有尖括号,就像它所做的那样。
与其一团糟,你可能想保留你的Tag
,只替换它的内容:
found.string.replace_with('stuff here')
请注意,正确的替换不会尝试覆盖标记。
当您执行found.replace_with(...)
时,名称found
引用的对象将在父层次结构中被替换。但是,名称found
一直指向与以前相同的过时对象。这就是为什么打印soup
显示更新,但打印found
不显示。