Using lxml:
import lxml.html as LH
import lxml.builder as builder
E = builder.E
doc = LH.parse('data')
footnote = doc.find('//div[@class="footnotes"]')
ol = E.ol()
for tag in footnote:
ol.append(tag)
footnote.append(ol)
print(LH.tostring(doc.getroot()))
prints
<html><body><div class="footnotes">
<ol><p>Footnote 1</p>
<p>Footnote 2</p>
</ol></div></body></html>
Note that with lxml
, an Element (tag) can be in only one place in the tree (since every Element has only one parent), so appending tag
to ol
also removes it from footnote
. So unlike with BeautifulSoup, you do not need to iterate over the contents in reverse order, nor use insert(0,...)
. You just append in order.
Using BeautifulSoup:
import bs4 as bs
with open('data', 'r') as f:
soup = bs.BeautifulSoup(f)
footnote = soup.find("div", { "class" : "footnotes" })
new_ol = soup.new_tag("ol")
for content in reversed(footnote.contents):
new_ol.insert(0, content.extract())
footnote.append(new_ol)
print(soup)
prints
<html><body><div class="footnotes"><ol>
<p>Footnote 1</p>
<p>Footnote 2</p>
</ol></div></body></html>