写了一个获取当前页面帖子标题的小程序练习一下
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 "分析结束!"
运行后的截图
骨头在选择方法的时候明显的比我老道 我写的太累赘了
没有评论:
发表评论