Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35800

Re: Noob trying to parse bad HTML using xml.etree.ElementTree

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'broken': 0.03; '"""': 0.05; 'preferably': 0.05; 'column': 0.07; 'python': 0.09; '""")': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:trying': 0.09; 'subject:using': 0.09; '<>.': 0.11; 'library': 0.15; 'properly': 0.15; 'framework,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'tweak': 0.16; 'wrote:': 0.17; 'trying': 0.21; 'import': 0.21; 'work.': 0.23; 'header:User-Agent:1': 0.26; 'question': 0.27; 'tree': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'dom': 0.29; 'implies': 0.29; 'included': 0.29; "i'm": 0.29; 'structure': 0.32; 'html,': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'similar': 0.35; 'received:org': 0.36; 'really': 0.36; 'closing': 0.36; 'loaded': 0.36; 'possible': 0.37; 'xml': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'easy': 0.60; 'real': 0.61; 'below.': 0.68; 'tags,': 0.81
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Noob trying to parse bad HTML using xml.etree.ElementTree
Date Sun, 30 Dec 2012 11:18:24 +0100
Organization None
References <CACQBTrQqJmNLAfvTpdxWMmLPy2kTQNrcDmNET_hA5c-8Axtm-Q@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a7b2.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1462.1356862716.29569.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356862716 news.xs4all.nl 6891 [2001:888:2000:d::a6]:58493
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35800

Show key headers only | View raw


Morten Guldager wrote:

> 'Aloha Friends!
> 
> I'm trying to process some HTML using xml.etree.ElementTree
> Problem is that the HTML I'm trying to read have some not properly closed
> tags, as the <img> shown in line 8 below.
> 
>   1 from xml.etree import ElementTree
>   2
>   3 tree = ElementTree
>   4 e = tree.fromstring(
>   5     """
>   6         <html>
>   7             <body>
>   8                 <img src='mogul.jpg'>
>   9             </body>
>  10         </html>
>  11     """)
> 
> Python whines: xml.etree.ElementTree.ParseError: mismatched tag: line 5,
> column 14
> 
> I definitely do want to work DOM style, having the whole shebang loaded
> into a nice structure before I start the real work.
> 
> Question is if it's possible to tweak xml.etree.ElementTree to accept, and
> understand sloppy html, or if you have suggestions for similar easy to use
> framework, preferably among the included batteries?

The <img> tag doesn't have a closing counterpart in HTML. That implies that 
valid HTML isn't valid XML and that you cannot use xml.etree with HTML.

While it is not in the standard library a good alternative for XML that can 
deal with HTML, too, is lxml. See <http://lxml.de/lxmlhtml.html>.
It also provides a way to cope with really broken html, modeled after 
BeautifulSoup.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Noob trying to parse bad HTML using xml.etree.ElementTree Peter Otten <__peter__@web.de> - 2012-12-30 11:18 +0100

csiph-web