python - 用 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
解决方案
我认为您可以简单地声明element.string
with =
。无需使用.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>
推荐阅读
- sql - 如何根据某些条件在postgres中逐列获取最后一行
- ios - 如何将 anchorPoint of view 设置为 0.5 、 0.5 以获得屏幕中心的视图?(不是场景)
- android - Flutter:相当于 NSLocale (iOS) & Locale (Android)
- centos - How to set up an additional glibc compiler on centOS
- java - 检查作业是否已经在运行,跳过新的 cron 实例作业
- python - How can I write a function fmap that returns the same type of iterable that was inputted?
- python - 在 Windows 上使用 Python 通过 ssh 连接到 DB 时出错
- razor - 颤振中是否有等效的 Razor View 引擎?
- python - 无法从 pantip.com 提取数据
- python-3.x - 并非所有点都在 Scikit-Optimize 中的空间误差范围内