使用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
#crifan
regex_title = '(?P.+?)';
title = re.search(regex_title, htmlcontent);
if(title):
title = title.group("title");
print "书籍名字:",title;
file_object.write('书籍名字:'+title+'r');
#书籍大小:27.2MB
filesize = re.search('(?P.+?)', htmlcontent);
if(filesize):
filesize = filesize.group("filesize");
print "文件大小:",filesize;
file_object.write('文件大小:'+filesize+'r');
#酷云中国电信下载
downurl1 = re.search('酷云中国电信下载 ', htmlcontent);
if(downurl1):
downurl1 = downurl1.group("downurl1");
print "下载地址1:",downurl1;
file_object.write('下载地址1:'+downurl1+'r');
sys.stdout.write('rFetching ' + title + '...n')
title = title.replace(' ', '');
title = title.replace('/', '');
saveFile = '/Users/superl/Desktop/pythonbook/'+title+'.rar';
if os.path.exists(saveFile):
print "该文件已经下载了!";
else:
urllib.urlretrieve(downurl1, saveFile, reporthook=report);
sys.stdout.write("rDownload complete, saved as %s" % (saveFile) + 'nn')
sys.stdout.flush()
file_object.write('文件下载成功!r');
else:
print "下载地址1不存在";
file_error.write(url+'r');
file_error.write(title+"下载地址1不存在!文件没有自动下载!r");
file_error.write('r');
#百度网盘下载2
downurl2 = re.search('百度网盘下载2 ', htmlcontent);
if(downurl2):
downurl2 = downurl2.group("downurl2");
print "下载地址2:",downurl2;
file_object.write('下载地址2:'+downurl2+'r');
else:
#file_error.write(url+'r');
print "下载地址2不存在";
file_error.write(title+"下载地址2不存在r");
file_error.write('r');
file_object.write('r');
print "n";
def getBooksUrl(url):
htmlcontent = getHtml(url);
#
urls = re.findall('", url
if __name__=="__main__":
file_object = open('/Users/superl/Desktop/python.txt','w+');
file_error = open('/Users/superl/Desktop/pythonerror.txt','w+');
pagenum = 3;
for pagevalue in range(1,pagenum+1):
listurl = "//www.jb51.net/ books/list476_%d.html"%pagevalue;
print listurl;
file_object.write(listurl+'r');
getBooksUrl(listurl);
file_object.close();
file_error.close(); 另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
名称栏目:使用python采集脚本之家电子书资源并自动下载到本地的实例脚本-创新互联
分享路径:http://www.jxjierui.cn/article/dijhco.html


咨询
建站咨询
