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


Posted in Python onOctober 23, 2018

3water上面的资源还比较全,就准备用python来实现自动采集信息,与下载啦。

Python具有丰富和强大的库,使用urllib,re等就可以轻松开发出一个网络信息采集器!

下面,是我写的一个实例脚本,用来采集某技术网站的特定栏目的所有电子书资源,并下载到本地保存!

软件运行截图如下:

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

在脚本运行时期,不但会打印出信息到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.3water.com:81/201512/books/JavaWeb100(3water.com).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 = "//3water.com"+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 = "//3water.com/ books/list476_%d.html"%pagevalue;
 print listurl;
 file_object.write(listurl+'r');
 getBooksUrl(listurl);
 file_object.close();
 file_error.close();

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

总结

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

Python 相关文章推荐
Python爬虫:通过关键字爬取百度图片
Feb 17 Python
matlab中实现矩阵删除一行或一列的方法
Apr 04 Python
对python中raw_input()和input()的用法详解
Apr 22 Python
python 将print输出的内容保存到txt文件中
Jul 17 Python
python2与python3中关于对NaN类型数据的判断和转换方法
Oct 30 Python
用python做游戏的细节详解
Jun 25 Python
安装好Pycharm后如何配置Python解释器简易教程
Jun 28 Python
keras自定义回调函数查看训练的loss和accuracy方式
May 23 Python
python学习将数据写入文件并保存方法
Jun 07 Python
python中数字是否为可变类型
Jul 08 Python
pandas 按日期范围筛选数据的实现
Feb 20 Python
Django 如何实现文件上传下载
Apr 08 Python
Python读取mat文件,并保存为pickle格式的方法
Oct 23 #Python
Python读取系统文件夹内所有文件并统计数量的方法
Oct 23 #Python
Python实现按逗号分隔列表的方法
Oct 23 #Python
Python解析Excle文件中的数据方法
Oct 23 #Python
使用python对excle和json互相转换的示例
Oct 23 #Python
Python实现将Excel转换成为image的方法
Oct 23 #Python
python pandas实现excel转为html格式的方法
Oct 23 #Python
You might like
PHP SQLite类
2009/05/07 PHP
Yii实现单用户博客系统文章详情页插入评论表单的方法
2015/12/28 PHP
Joomla使用Apache重写模式的方法
2016/05/04 PHP
ThinkPHP框架实现导出excel数据的方法示例【基于PHPExcel】
2018/05/12 PHP
jQuery 获取和设置select下拉框的值实现代码
2013/11/08 Javascript
JavaScript中对DOM节点的访问、创建、修改、删除
2015/11/16 Javascript
超精准的javascript验证身份证号的具体实现方法
2015/11/18 Javascript
WdatePicker.js时间日期插件的使用方法
2017/07/26 Javascript
详解Angular4 路由设置相关
2017/08/26 Javascript
详解Vue.js iview实现树形权限表(可扩展表)
2018/09/30 Javascript
浅谈Webpack4 Tree Shaking 终极优化指南
2019/11/18 Javascript
Vue CLI3移动端适配(px2rem或postcss-plugin-px2rem)
2020/04/27 Javascript
vue基础知识--axios合并请求和slot
2020/06/04 Javascript
[05:49]2014DOTA2TI4正赛第二日综述 昔日冠军纷纷落马 VG LGD占尽先机
2014/07/20 DOTA
Python实现单词拼写检查
2015/04/25 Python
python清除字符串里非字母字符的方法
2015/07/02 Python
全面了解python中的类,对象,方法,属性
2016/09/11 Python
python组合无重复三位数的实例
2018/11/13 Python
django框架两个使用模板实例
2019/12/11 Python
Django Path转换器自定义及正则代码实例
2020/05/29 Python
安装python依赖包psycopg2来调用postgresql的操作
2021/01/01 Python
HTML5印章绘制电子签章图片(中文英文椭圆章、中文英文椭圆印章)
2019/06/03 HTML / CSS
Lookfantastic挪威官网:英国知名美妆购物网站
2017/07/26 全球购物
包装类的功能、种类、常用方法
2012/01/27 面试题
css animation配合SVG制作能量流动效果
2021/03/24 HTML / CSS
应届大学生自荐信格式
2013/09/21 职场文书
《祁黄羊》教学反思
2014/04/22 职场文书
小学生春游活动方案
2014/08/20 职场文书
全国法院系统开展党的群众路线教育实践活动综述(全文)
2014/10/25 职场文书
2014年小学体育工作总结
2014/12/11 职场文书
英语辞职信范文
2015/02/28 职场文书
三八节活动主持词
2015/07/04 职场文书
Python中requests做接口测试的方法
2021/05/30 Python
springboot + mongodb 通过经纬度坐标匹配平面区域的方法
2021/11/01 MongoDB
MySQL磁盘碎片整理实例演示
2022/04/03 MySQL
vue项目打包后路由错误的解决方法
2022/04/13 Vue.js