python - 如何使用 Beautiful Soup 查找和更改标签之外的文本?

标签 python python-3.x beautifulsoup

我有一个这样的文件:

words1 outside of a Tag <tag1> words2 inside of tag1 </tag1> words3 outside of a Tag

我想提取 tag1 之外的字符串,并使用 beautifulsoup 将其更改为如下所示:

changed word1 <tag1> words2 inside of tag1 </tag1> changed word3

如何用 beautifulSoup 替换标签中的单词?

最佳答案

文本元素也被视为父元素的子元素。

如果找到 tag1,则可以在属性 .previousSibling.nextSibling 中找到前后文本。或者,您可以找到父标签,然后选择适当的子标签。

示例:

from bs4 import BeautifulSoup
# assuming BeautifulSoup 4

doc = """
words1 outside of a Tag <tag1>words2 inside of tag1</tag1>
words3 outside of a Tag
"""

soup = BeautifulSoup(doc, 'html.parser')
tag = soup.find('tag1')
tag.previousSibling.replaceWith('changed word1 ')
tag.nextSibling.replaceWith(' changed word3')

print(soup)

关于python - 如何使用 Beautiful Soup 查找和更改标签之外的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45757641/

相关文章:

python - 如何获取页面内的直接下载链接?

python - 父标签的子标签有特定属性值时,如何使用BeautifulSoup获取父标签名称值?

python - table 和汤的问题

python - numpy 已与 Anaconda 一起安装,但我收到 ImportError(DLL 加载失败 : The specified module could not be found)

python - 将 JS 变量发布到 Django View 并在单独的模板中显示为上下文变量

python-3.x - 'for' 循环中未定义“var”

python-3.x - 从 Pandas 构建一个方法词典

python - Django:根据用户显示不同的内容

python - 对 Pandas Dataframes 中的列数据进行分组

python-3.x - Pyspark 数据帧,基于组在标志之间迭代