tags:

views:

68

answers:

2

I have created a simple test harness in python for my ASP .net web site.

I would like to look up some HTML tags in the resulting page to find certain values.\

What would be the best way of doing this in python?

eg (returned page):

<div id="ErrorPanel">An error occurred......</div>

would display (in std out from python):

Error: .....

or

<td id="dob">23/3/1985</td>

would display:

Date of birth: 23/3/1985
+3  A: 

Do you want to parse XML, as you state in your question's title, or HTML, as you show in the text of the question? For the latter, I recommend BeautifulSoup -- download it and install it, then, once having made a soup object out of the HTML, you can easily locate the tag with a certain id (or other attribute), e.g.:

errp = soup.find(attrs={'id': 'ErrorPanel'})
if errp is not None:
  print 'Error:', errp.string

and similarly for the other case (easily tweakable e.g. into a loop if you're looking for non-unique attributes, and so on).

Alex Martelli
I would like to parse HTML (XHTML).
Russell
Keep in mind, Beautiful Soup is designed to parse sloppy HTML, and it might not work on theoretically correct XHTML. On the other hand, a correct XML parser will choke on bad inputs that Beautiful Soup will parse.
wisty
@wisty, I've never had any problem using BeautifulSoup to parse XHTML -- can you think of any specific example? (URL would be welcome, tx!)
Alex Martelli
+3  A: 

You can also do it with lxml. It handles HTML very well, and you can use CSS selectors for querying DOM, which makes it particularly attractive if you use libraries like jQuery regularly.

Imran