分享

用python来为自己办事

 IT女女 2016-01-28

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


import sys,urllib
url="http://www./html/radio/VOA/presidentspeech/index.html"
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 ="http://www./"+content
print content
print url
wp = urllib.urlopen(url)
print "start download..."
content = wp.read()

#print content
print content.count("<div class=\"content\"")
#content = content[content.find("<div class=\"content\""):]
content = content[content.find("<!--info end------->"):]
content = content[:content.find("<div class=\"dede_pages\"")-1]
filename = filename[filename.find("presidentspeech")+len("presidentspeech/"):]

filename = filename.replace('/',"-",filename.count("/"))
fp = open(filename,"w+")
fp.write(content)
fp.close()
print content                    

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多