使用python采集脚本之家电子书资源并自动下载到本地的实例脚本

 更新时间:2018年10月23日 15:58:26   作者:忘忧草论坛  
这篇文章主要介绍了python采集jb51电子书资源并自动下载到本地实例教程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

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
 #<h1 class="h1user">crifan</h1>
 regex_title = '<h1s+?itemprop="name">(?P<title>.+?)</h1>';
 title = re.search(regex_title, htmlcontent);
 if(title):
 title = title.group("title");
 print "书籍名字:",title;
 file_object.write('书籍名字:'+title+'r');
 #<li>书籍大小:<span itemprop="fileSize">27.2MB</span></li>
 filesize = re.search('<spans+?itemprop="fileSize">(?P<filesize>.+?)</span>', htmlcontent);
 if(filesize):
 filesize = filesize.group("filesize");
 print "文件大小:",filesize;
 file_object.write('文件大小:'+filesize+'r');
 #<div class="picthumb"><a href="//img.jbzj.com/do/uploads/litimg/151210/1A9262GO2.jpg" target="_blank"
 bookimg = re.search('<divs+?class="picthumb"><a href="(?P<bookimg>.+?)" rel="external nofollow" target="_blank"', htmlcontent);
 if(bookimg):
 bookimg = bookimg.group("bookimg");
 print "封面图片:",bookimg;
 file_object.write('封面图片:'+bookimg+'r');
 #<li><a href="http://xz6.jb51.net:81/201512/books/JavaWeb100(jb51.net).rar" target="_blank">酷云中国电信下载</a></li>
 downurl1 = re.search('<li><a href="(?P<downurl1>.+?)" rel="external nofollow" target="_blank">酷云中国电信下载</a></li>', 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');
 #<li><a href="http://pan.baidu.com/s/1pKfVNwJ" rel="external nofollow" target="_blank">百度网盘下载2</a></li>
 downurl2 = re.search('</a></li><li><a href="(?P<downurl2>.+?)" rel="external nofollow" target="_blank">百度网盘下载2</a></li>', 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);
 #<ul class="cur-cat-list"><a href="/books/438381.html" rel="external nofollow" class="tit"</ul></div><!--end #content -->
 urls = re.findall('<a href="(?P<urls>.+?)" rel="external nofollow" class="tit"', htmlcontent);
 for url in urls:
 url = "//www.jb51.net"+url;
 print url+"n";
 file_object.write(url+'r');
 getBookInfo(url)
 #print "url->", 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();

注意,上面代码部分地方的url被我换了。

总结

以上所述是小编给大家介绍的python采集jb51电子书资源并自动下载到本地实例脚本,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • python协程之动态添加任务的方法

    python协程之动态添加任务的方法

    今天小编就为大家分享一篇python协程之动态添加任务的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • 详解Python中数据库管理模块shelve和dbm的应用

    详解Python中数据库管理模块shelve和dbm的应用

    作为常用的 python 自带数据库管理模块,shelve 和 dbm 都是非常方便的对象持久化存储和检索工具,本文将从用法、优势以及不同点等方面进行介绍,希望对大家有所帮助
    2023-10-10
  • Python编程pytorch深度卷积神经网络AlexNet详解

    Python编程pytorch深度卷积神经网络AlexNet详解

    AlexNet和LeNet的架构非常相似。这里我们提供了一个稍微精简版本的AlexNet,去除了当年需要两个小型GPU同时运算的设计特点
    2021-10-10
  • python上selenium的弹框操作实现

    python上selenium的弹框操作实现

    这篇文章主要介绍了python上selenium的弹框操作实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • DJANGO-ALLAUTH社交用户系统的安装配置

    DJANGO-ALLAUTH社交用户系统的安装配置

    django-allauth是集成了local用户系统和social用户系统,其social用户系统可以挂载多个账户。也是一个流行度非常高的Django user系统,我们这里简单介绍下,分享下个人的使用经验
    2014-11-11
  • python中argparse模块及action='store_true'详解

    python中argparse模块及action='store_true'详解

    argparse 是一个用来解析命令行参数的 Python 库,它是 Python 标准库的一部分,这篇文章主要介绍了python中argparse模块及action=‘store_true‘详解,需要的朋友可以参考下
    2023-02-02
  • 利用Python将数值型特征进行离散化操作的方法

    利用Python将数值型特征进行离散化操作的方法

    今天小编就为大家分享一篇利用Python将数值型特征进行离散化操作的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-11-11
  • python三大器之迭代器、生成器、装饰器

    python三大器之迭代器、生成器、装饰器

    迭代是Python最强大的功能之一,是访问集合元素的一种方式;迭代器是一个可以记住遍历的位置的对象,本文给大家介绍python三大器之迭代器、生成器、装饰器的相关知识,感兴趣的朋友跟随小编一起看看吧
    2022-01-01
  • Python读写Excel文件方法介绍

    Python读写Excel文件方法介绍

    这篇文章主要介绍了Python读写Excel文件方法介绍,本文讲解了xlrd、xlwt、xlutils等类库的使用,需要的朋友可以参考下
    2014-11-11
  • Python Selenium异常处理的实例分析

    Python Selenium异常处理的实例分析

    在本篇内容里小编给大家分享了关于Python Selenium异常处理的实例分析内容,对此有兴趣的朋友们可以学习参考下。
    2021-02-02

最新评论