I get malformed HTML input with divs inside other HTML elements like the html_string variable in the below code. As this is not valid HTML, parsers give me unexpected results and it can't be used logically.

I am trying to fix this HTML by reading it as an XML initially using BeautifulSoup and then re-positioning the div to the body element which is where it belongs in my case.

Understanding that this is a valid XML but invalid HTML is one of the the keys to this solution. Can anyone review this code?

This is used to fix page-break divs that come from a certain source and it is not a regular HTML.

import bs4

html_string = """
  <p align="center">
  This is before.
  <div style="page-break-after:always">
  This is after.

html_element = bs4.BeautifulSoup(html_string, features="xml")

style = {'style': 'page-break-after:always'}

page_break_elements = html_element.findAll('div', style)

for page_break_element in page_break_elements:
    current = page_break_element
    while True:
        parent = current.parent
        if parent is None:
        if parent.name == 'body':
        current = parent
  • \$\begingroup\$ This seems more to be a StackOverflow question. Does your code actually work? \$\endgroup\$
    – user86624
    Aug 2 '16 at 10:46
  • \$\begingroup\$ Yeah it works. I validated it. Wanted feedbacks. \$\endgroup\$
    – Nishant
    Aug 3 '16 at 11:26
  • \$\begingroup\$ I am more worried about negative use cases that could cause havoc. Can anyone think of anything. \$\endgroup\$
    – Nishant
    Aug 5 '16 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.