python 面向对象入门 - 之 正则表达式

该博文涵盖了如下内容:
1.正则表达式 re
2.url库 urllib
3.debug 方法
4.面向对象封装方法

#encoding=utf-8
'''
python learn regular express
url : http://docs.python.org/library/re.html
parse html url : http://www.boddie.org.uk/python/HTML.html
author : liuzheng
'''
import re
import urllib

#分析javaeye blog 频道
class ParseHTML:
'''
parse html for infomation
parse javeeye page
'''

def __init__(self,url):
self.url = url
pass


#analyses html
def parse(self):
sock = urllib.urlopen(self.url)
html = sock.read()
self.__puts(html)
pass

#打印html 匹配数据
def __puts(self,html):
b = re.compile(r"<a href='([\w./:\\]+?)'[\s]*title=([^<>]+?)[\s]*target=([^<>]+?)>([^<>]+?)</a>",re.I)
m = re.findall(b,html)
#这里有encode 问题?,不知道,大家是否可以帮忙解答
print m


if __name__ == '__main__':
url = "http://www.iteye.com/blogs"
p = ParseHTML(url)
p.parse()


if __debug__:
print "debuging is %s" % __debug__
print "regular" + "* " * 30
#math
str = "800-820-8800"
m = re.match(r"(\d{3})-(\d{3})-(\d{4})", str)
print "result : " ,m.groups()

#split
print "split : %s" % re.split('\W', 'Words, words, words.')

#findall
text = "He was carefully disguised but captured quickly by police."
print "findall :%s" % re.findall(r"\w+ly",text)

#sub
text = "hello world!"
print "sub:%s" % re.sub(r"\s+","--",text)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值