首页 > 解决方案 > 用 Beautifulsoup 替换 p 标签的内容

问题描述

我在替换内容时遇到了问题,当 html 包含以下内容时会出现问题:

    <p>Next, go to your <strong>/home/pi</strong> directory and check if you can see the picture</p>

我想做的是替换 p 标签的内容,同时丢弃任何额外的样式或里面的标签。在此示例中,这意味着强标签将不再是新字符串的一部分。

但是,我发现完全替换 p 标签的内容是不可能的。我已经用谷歌搜索了我的问题/错误,但无法提出一个可行的示例。

这是我的代码和我尝试运行的测试,有些会抛出错误,有些则根本不做任何事情。您可以取消引用其中任何一个来自己测试它,但结果已经附加在评论中。


from bs4 import BeautifulSoup

src = "<p>Next, go to your <strong>/home/pi</strong> directory and check if you can see the picture</p>"

soup=BeautifulSoup(src, "lxml")

for element in soup.findAll():
    if element.name == 'p':
        print(element) 
        #= <p>Next, go to your <strong>/home/pi</strong> directory and check if you can see the picture</p>
        print(element.text) 
        #= Next, go to your /home/pi/ directory and check if you can see the picture
        print(element.contents) 
        #= ['Next, go to your ', <strong>/home/pi</strong>, ' directory and check if you can see the picture']

        # -- test 1:
        # element.string.replace_with("First, go to your /home/pi directory")
        # AttributeError: 'NoneType' object has no attribute 'replace_with'

        # -- test 2:
        # element.replace("First, go to your /home/pi directory")
        # TypeError: 'NoneType' object is not callable

        # -- test 3:
        # new_tag = soup.new_tag('li')
        # new_tag.string = "First, go to your /home/pi directory"
        # element.replace_with(new_tag)
        # print(element)
        # not replaced

        # -- test 4:
        # element.text.replace(str(element), "First, go to your /home/pi directory")
        # print(element)
        # not replaced

        # -- test 5:
        # element.text.replace(element.text, "First go to your /home/pi/ directory")
        # print(element)
        # not replaced

        # -- test 6:
        new_tag = soup.new_tag('li')
        new_tag.string = "First, go to your /home/pi directory"
        element.replaceWith(new_tag)
        print(element)
        # not replaced

        # -- test 7:
        # element.replace_with("First, go to your /home/pi directory")
        # print(element)
        # not replaced

我怀疑问题是由于element.contents包含多个项目而发生的。但是,element.text它为我提供了处理字符串并替换它所需的东西,而且我不关心里面的任何样式。

作为最后的手段,我将从str.replace格式化的 html 中获取元素,但如果可能的话,我更愿意在 BeautifulSoup 中处理这个问题。

使用的来源:

https://www.tutorialfor.com/questions-59179.htm https://beautiful-soup-4.readthedocs.io/en/latest/#modifying-the-tree https://www.crummy.com/software /BeautifulSoup/bs4/doc/#making-the-soup https://www.crummy.com/software/BeautifulSoup/bs4/doc/#replace-with https://www.crummy.com/software/BeautifulSoup/bs4 /doc/#method-names

AttributeError:“NoneType”对象没有属性“replace_with”

https://stackoverflow.com/a/25722910/8623540

标签: pythonhtmlbeautifulsouphtml-parsing

解决方案


我认为您可以简单地声明element.stringwith =。无需使用.replace()

from bs4 import BeautifulSoup

src = "<p>Next, go to your <strong>/home/pi</strong> directory and check if you can see the picture</p>"

soup=BeautifulSoup(src, "html.parser")
print ('Original: %s' %soup)

for element in soup.findAll():
    if element.name == 'p':
        element.string = "First, go to your /home/pi directory"

print('Altered: %s' %soup)

输出:

Original: <p>Next, go to your <strong>/home/pi</strong> directory and check if you can see the picture</p>
Altered: <p>First, go to your /home/pi directory</p>

推荐阅读