网站自动采集文章(python采集jb51电子书资源并自动下载到本地实例下载啦)

优采云 发布时间: 2021-12-10 11:00

  网站自动采集文章(python采集jb51电子书资源并自动下载到本地实例下载啦)

  本文文章主要介绍python采集jb51电子书资源并自动下载到本地实例教程。很好,有一定的参考价值。有需要的朋友可以参考。

  jb51上的资源比较齐全,打算用python实现自动采集信息下载。

  Python 拥有丰富而强大的库。使用urllib、re等可以轻松开发一个网络信息采集器

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

  软件运行截图如下:

  

  脚本运行时不仅会将信息打印到shell窗口,还会将日志保存到txt文件中,记录采集的页地址、书名和大小、本地服务器下载地址和百度网盘下载地址!

  示例采集并下载html中文网站的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'); #

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线