【发布时间】:2020-10-05 04:29:36
【问题描述】:
我正在解析 html 文件并用新标签替换特定链接。
Python 代码:
from bs4 import BeautifulSoup
sample='''<a href="{Image src='https://google.com' link='https://google.com'}" >{Image src='https://google.com' link='google.com'}</a>'''
soup=BeautifulSoup(sample)
for a in soup.findAll('a'):
x=BeautifulSoup('<ac:image><ri:attachment ri:filename="somefile"/> </ac:image>')
a=a.replace_with(x)
print(soup)
实际输出:
<ac:image><ri:attachment ri:filename="somefile"></ri:attachment> </ac:image>
所需的输出:
<ac:image><ri:attachment ri:filename="somefile" /></ac:image>
自动关闭标签会自动转换。目的地严格需要自闭标签。
任何帮助将不胜感激!
【问题讨论】:
-
您忘记包含代码,请将其添加到帖子中。
-
您可以编辑您的问题并发布输入/您的代码吗?
-
@Sushanth ,添加了所需输出和实际输出的代码。
标签: python html python-3.x beautifulsoup tags