【发布时间】:2014-05-03 03:56:58
【问题描述】:
我想用 BeautifulSoup 包装标签的内容。 这个:
<div class="footnotes">
<p>Footnote 1</p>
<p>Footnote 2</p>
</div>
应该变成这样:
<div class="footnotes">
<ol>
<p>Footnote 1</p>
<p>Footnote 2</p>
</ol>
</div>
所以我使用以下代码:
footnotes = soup.findAll("div", { "class" : "footnotes" })
footnotes_contents = ''
new_ol = soup.new_tag("ol")
for content in footnotes[0].children:
new_tag = soup.new_tag(content)
new_ol.append(new_tag)
footnotes[0].clear()
footnotes[0].append(new_ol)
print footnotes[0]
但我得到以下信息:
<div class="footnotes"><ol><
></
><<p>Footnote 1</p>></<p>Footnote 1</p>><
></
><<p>Footnote 2</p>></<p>Footnote 2</p>><
></
></ol></div>
建议?
【问题讨论】:
-
我可以用 lxml 为你试试这个吗?
-
请。没问题。
标签: python beautifulsoup lxml