3

I've a string of HTML elements

HTMLstr = """
    <div class='column span4 ui-sortable' id='column1'></div>
    <div class='column span4 ui-sortable' id='column2'>
        <div class='portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all' id='widget_basicLine'>
        <div class='portlet-header ui-widget-header ui-corner-all'><span class='ui-icon ui-icon-minusthick'></span>Line Chart </div>
        <div class='portlet-content' id=basicLine style='height:270px; margin: 0 auto;'></div>          
        </div>
    </div>
    <div class='column span4 ui-sortable' id='column3'></div> """

I want to convert the above HTML string into respective HTML DOM elements in python?

I can do it in jQuery/AJAX function via $(this).html(HTMLstr); but how do I parse it in python?

5
  • Have you got any python code you've tried? Any libraries you've already looked at? What worked, what didn't?
    – user764357
    Commented Sep 10, 2013 at 5:19
  • try html dom parser. thehtmldom.sourceforge.net Commented Sep 10, 2013 at 5:21
  • I havent used any library yet. I'm sending the HTMLstr from backend(python) to the front end. I want to parse the HTMLstr into its equivalent HTMLDOM elements from the python only. How can I do it? Commented Sep 10, 2013 at 5:22
  • @Pradeeshnarayan - This could've worked but I'm using python 2.7. and it requires python 3.2 Commented Sep 10, 2013 at 5:23
  • 2
    -1: Given the comments from OP on the answers, OP did not actually want to do anything in Python and does not really seem to understand what they want.
    – pkh
    Commented Sep 10, 2013 at 18:03

2 Answers 2

6

Python has built-in libraries for parsing HTML documents. In Python 2.x, you have your choice of HTMLParser (recommended) and htmllib (deprecated); in Python 3.x, html.parser is the appropriate library (this is a renamed version of HTMLParser from Python 2.x).

However, these are event-driven parsers (similar to XML SAX parsers), which may not be what you want. An alternative would be using one of Python's XML parsing tools, if you know that the document is going to be valid XML (i.e. tags properly closed, etc.). The libraries xml.dom and xml.dom.minidom are both options, depending on what kind of parsing you're looking for (I suspect xml.dom.minidom is sufficient for your purposes, given your example).

For example, you should be able to enter this in your Python console and get the output shown:

>>> import xml.dom.minidom
>>> x = xml.dom.minidom.parseString('<div class="column span4 ui-sortable" id="column2"><div class="portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" id="widget_basicLine" /></div>')
>>> x.documentElement.nodeName
'div'
>>> x.documentElement.getAttribute("class")
'column span4 ui-sortable'
>>> len(x.documentElement.firstChild.childNodes)
0

A full description of the Node objects you receive is available here. If you're used to using the DOM in JavaScript, you should find that most of the properties are the same. Note that because Python treats this as an XML document, HTML-specific properties like 'class' have no special significance, so I believe you have to use the getAttribute function to access them.

7
  • The Python documentation has an example here. However, it occurs to me that HTMLParser is event-driven rather than the DOM parser from your example, so you might be better off with one of the XML parsers. For xml.dom.minidom, the relevant calls are either xml.dom.minidom.parse (for files) or xml.dom.minidom.parseString (for strings of markup).
    – Ben S.
    Commented Sep 10, 2013 at 5:41
  • Still not able able to get the correct o/p required. Can you please provide a working example. Commented Sep 10, 2013 at 5:48
  • I've added an example above - hopefully this will help. If it doesn't, can you give me more details about the problem you are seeing?
    – Ben S.
    Commented Sep 10, 2013 at 6:14
  • I guess you havent understood my question. I have created a string(shown above) and I return this string from python to the front end. On the front-end browser, I get the plain text of string, instead I want the string to be parsed into equivalent HTML elements and render the output accordingly. How do I do that? Commented Sep 10, 2013 at 6:23
  • Well, you're right - I'm definitely confused. If what you want to do is take a string of text representing HTML and transform it into HTML elements to be inserted into the web page in jQuery, isn't that done with the expression $(this).html(HTMLstr); that you used when asking the question? If you want to use the output as HTML without going through this step, I'm not sure that's possible - you can use the responseXML property of the AJAX request object, but the elements would belong to a different document and in my experience browsers don't let you easily move them over.
    – Ben S.
    Commented Sep 10, 2013 at 6:31
2

You should use BeautifulSoup -- does exactly what you need.

http://www.crummy.com/software/BeautifulSoup/

4
  • I tried this and it returns the same string , not the HTML elements. from bs4 import BeautifulSoup htmlstr = "<div class='column span4 ui-sortable' id='column2'><div class='portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all' id='widget_basicLine'><div class='portlet-header ui-widget-header ui-corner-all'><span class='ui-icon ui-icon-minusthick'></span>Line Chart </div><div class='portlet-content' id=basicLine style='height:270px; margin: 0 auto;'></div></div></div>" soup = BeautifulSoup(htmlstr) return soup Commented Sep 10, 2013 at 5:46
  • What are you trying to do? How did you determine it's a string object and not some other more complex object? Commented Sep 10, 2013 at 5:59
  • I have created a string(shown above) and I return this string from python to the front end. On the front-end browser, I get the plain text of string, instead I want the string to be parsed into equivalent HTML elements and render the output accordingly. How do I do that? Commented Sep 10, 2013 at 6:23
  • Its a string object only. I've created this string of HTML elements as a string object. Now, I want to convert/parse this string into its equivalent HTML elements. How do I do that? Commented Sep 10, 2013 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.