2009年2月28日星期六

论坛监视器第一步——获取帖子标题

正则方面感觉和原来的小有区别 MS零宽断言没有了 零宽断言完成的任务让分组替代了 简单了很多

写了一个获取当前页面帖子标题的小程序练习一下

import urllib2,re

f=urllib2.urlopen(
'http://bbs.cfan.com.cn/forum-53-1.html#stickthread_787399')

fp=file(
'html.txt','w')

fp.write(f.read())

fp.close

reg1=
'(\s*<span\s*id="thread_\d*"><a\s*href="thread-\d*-\d*-\d*\.html">)(.*)(</a></span>)'

fp=file('html.txt','r')

while True:

    line = fp.readline()

    
if len(line) == 0:

        
break

    
reg2=line

    
try:

        reg=re.search(reg1,reg2).group(
2)

        
print reg

        
print

    except 
AttributeError:

        
continue

fp.close

print "分析结束!"


运行后的截图



骨头在选择方法的时候明显的比我老道 我写的太累赘了

没有评论:

发表评论