使用python采集脚本之家电子书资源并自动下载到本地的实例脚本 jb51上面的资源还比较全,就准备用python来实现自动采集信息,与下载啦。 Python具有丰富和强大的库,使用urllib,re等就可以轻松开发出一个网络信息采集器! 下面,是我写的一个实例脚本,用来采集某技术网站的特定栏目的所有电子书资源,并下载到本地保存! 软件运行截图如下: 在脚本运行时期,不但会打印出信息到shell窗口,还会保存日志到txt文件,记录采集到的页面地址,书籍的名称,大小,服务器本地下载地址以及百度网盘的下载地址! 实例采集并下载中文源码网的python栏目电子书资源: # -*- coding:utf-8 -*- import re import urllib2 import urllib import sys import os reload(sys) sys.setdefaultencoding('utf-8') def getHtml(url): request = urllib2.Request(url) page = urllib2.urlopen(request) htmlcontent = page.read() #解决中文乱码问题 htmlcontent = htmlcontent.decode('gbk', 'ignore').encode("utf8",'ignore') return htmlcontent def report(count, blockSize, totalSize): percent = int(count*blockSize*100/totalSize) sys.stdout.write("r%d%%" % percent + ' complete') sys.stdout.flush() def getBookInfo(url): htmlcontent = getHtml(url); #print "htmlcontent=",htmlcontent; # you should see the ouput html #