I am trying to go through the HTML of a website and parse it looking for the max enrollment of a class. I tried checking for a substring in each line of the HTML file, but that would try to parse the wrong lines. So I am now using Regular Expressions. I have \t\t\t\t\t\t\t
([0-9])|([0-9][0-9])|([0-9][0-9][0-9])\r\n as my regular expression right now, but this regular expression matches the max enrollment as well as the section number. Is there another way to go about what I am trying to extract from the webpage? The HTML code snippet is below:Section001Credits 4.00TitleLinear AlgebraCampusUniversity CityInstructor(s)Guang YangInstruction TypeLectureMax Enroll30解决方案
Use the right tool for the right job.
Let's make an analogy to explain why it's wrong: it's like trying to have a 5 year old understand Hamlet, whereas he does not have the vocabulary and grammar to understand Shakespeare's, that he will get when he'll be able to process more abstract concepts.
Use either lxml or BeautifulSoup to do that.
As an example: to get a list of all the evens and all the odds:
>>> from lxml import etree
>>> tree = etree.HTML(your_html_text)
>>> odds = tree.xpath('//td[@class="odd"]/text()')
>>> evens = tree.xpath('//td[@class="even"]/text()')
>>> odds
['001', 'Linear Algebra', 'Guang Yang', '30']
>>> evens
[' 4.00', 'University City', 'Lecture']
edit:
I am just trying to extract the contents in such a way where I don't get the section number AND max enroll number. I just need help with getting only the Max Enroll number.
ok, now I'm getting what you want, so here's the solution using lxml:
>>> for elt in tree.xpath('//tr'):
... if elt.xpath('td[@class="tableHeader"]')[0].text == "Max Enroll":
... elt.xpath('td[@class="odd"]|td[@class="even"]')[0].text
...
'30'
There you have only the max enroll number.
Using BeautifulSoup it's a bit easier:
>>> bs = BeautifulSoup(your_html_text)
>>> for t in bs.findAll('td', attrs={'class': 'tableHeader'}):
... if t.text == "Max Enroll":
... print t.findNext('td').text
'30'