Parsing HTML using Python Parsing HTML using Python python python

Parsing HTML using Python


So that I can ask it to get me the content/text in the div tag with class='container' contained within the body tag, Or something similar.

try:     from BeautifulSoup import BeautifulSoupexcept ImportError:    from bs4 import BeautifulSouphtml = #the HTML code you've written aboveparsed_html = BeautifulSoup(html)print(parsed_html.body.find('div', attrs={'class':'container'}).text)

You don't need performance descriptions I guess - just read how BeautifulSoup works. Look at its official documentation.


I guess what you're looking for is pyquery:

pyquery: a jquery-like library for python.

An example of what you want may be like:

from pyquery import PyQuery    html = # Your HTML CODEpq = PyQuery(html)tag = pq('div#id') # or     tag = pq('div.class')print tag.text()

And it uses the same selectors as Firefox's or Chrome's inspect element. For example:

the element selector is 'div#mw-head.noprint'

The inspected element selector is 'div#mw-head.noprint'. So in pyquery, you just need to pass this selector:

pq('div#mw-head.noprint')


Here you can read more about different HTML parsers in Python and their performance. Even though the article is a bit dated it still gives you a good overview.

Python HTML parser performance

I'd recommend BeautifulSoup even though it isn't built in. Just because it's so easy to work with for those kinds of tasks. Eg:

import urllib2from BeautifulSoup import BeautifulSouppage = urllib2.urlopen('http://www.google.com/')soup = BeautifulSoup(page)x = soup.body.find('div', attrs={'class' : 'container'}).text