用python来为自己办事-抓取网页内容

20547阅读 0评论2009-12-18 ruanbanyong1
分类:Python/Ruby

每天早上都要听美国总统电台演说,都没有保存资料。昨天上班上的郁闷特写一个python脚本自动抓取并保存该资料,python真是强大。从学到整个东西写完只用了1个多小时,去掉页面上的大量广告,只留下所需信息,自动保存为html文件。程序很简单,就不错说了看代码


import sys,urllib
url=""
wp = urllib.urlopen(url)
print "start download..."
content = wp.read()


print content.count("center_box")
index =  content.find("center_box")
content=content[content.find("center_box")+1:]
content=content[content.find("href=")+7:content.find("target")-2]
filename = content
url =""+content
print content
print url
wp = urllib.urlopen(url)
print "start download..."
content = wp.read()

#print content
print content.count("
#content = content[content.find("
content = content[content.find(""):]
content = content[:content.find("
filename = filename[filename.find("presidentspeech")+len("presidentspeech/"):]

filename = filename.replace('/',"-",filename.count("/"))
fp = open(filename,"w+")
fp.write(content)
fp.close()
print content
上一篇:python 字符串操作
下一篇:字节对齐