python读取html文件正则替换,Python使用正则表达式解析HTML

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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值