自动采集文章( 2018年python采集jb51电子书资源并自动下载到本地实例脚本)
优采云 发布时间: 2022-01-15 23:19自动采集文章(
2018年python采集jb51电子书资源并自动下载到本地实例脚本)
使用python采集Script House电子书资源并自动下载到本地示例脚本
更新时间:2018-10-23 15:58:26 作者:网游草论坛
本文章主要介绍python采集jb51电子书资源,自动下载到本地示例教程。非常好,有一定的参考价值。有需要的朋友可以参考以下
jb51上的资源还是比较齐全的,所以打算用python实现自动采集信息,下载下来。
Python拥有丰富强大的库,使用urllib、re等可以轻松开发出网络资料采集器!
下面是我写的一个示例脚本,使用采集某技术网站特定栏目的所有电子书资源,下载保存到本地!
软件运行截图如下:
脚本运行时,不仅会将信息打印到shell窗口,还会将日志保存为txt文件,记录采集的页面地址,书名和大小,本地服务器下载地址和百度网盘下载地址!
示例采集并下载脚本之家python专栏的电子书资源:
<p>
# -*- 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');
#