我有很多HTML页面,其中包含这些代码片段的各个部分:
<div class="footnote" id="footnote-1">
<h3>Reference:</h3>
<table cellpadding="0" cellspacing="0" class="floater" style="margin-bottom:0;" width="100%">
<tr>
<td valign="top" width="20px">
<a href="javascript:void(0);" onclick='javascript:toggleFootnote("footnote-1");' title="click to hide this reference">1.</a>
</td>
<td>
<p> blah </p>
</td>
</tr>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以成功解析HTML并提取这些相关标签
tags = soup.find_all(attrs={"footnote"})
Run Code Online (Sandbox Code Playgroud)
现在我需要添加关于这些的新父标记,以便代码片段:
<div class="footnote-out"><CODE></div>
Run Code Online (Sandbox Code Playgroud)
但我找不到在bs4中添加父标签的方法,以便它们支持已识别的标签.insert()/ insert_before在标识的标记之后添加.
我开始尝试字符串操作:
for tags in soup.find_all(attrs={"footnote"}):
tags = BeautifulSoup("""<div class="footnote-out">"""+str(tags)+("</div>"))
Run Code Online (Sandbox Code Playgroud)
但我相信这不是最好的课程.
谢谢你的帮助.刚开始使用bs/bs4但似乎无法解决这个问题.