我们专注孝感网站设计 孝感网站制作 孝感网站建设
成都网站建设公司服务热线:18982081108

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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

jb51上面的资源还比较全,就准备用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
 #

crifan

regex_title = '(?P.+?)</h2>'; 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();</pre><p align=center>另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、<a href="https://www.cdcxhl.com/gaofang/" target="_blank">高防服务器</a>、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。</p> <br> 当前标题:使用python采集脚本之家电子书资源并自动下载到本地的实例脚本-创新互联 <br> 文章路径:<a href="http://www.ytofo.com/article/dijhco.html">http://www.ytofo.com/article/dijhco.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/djgiphh.html">脚本小子下载(到底有没有黑客)</a> </li><li> <a href="/article/djgipcg.html">大优点10大关于Linux的优点:让你了解其以及它的能力(linux的10)</a> </li><li> <a href="/article/djgihdd.html">ios16.6.1适合哪些机型「ios16.1值得更新吗」</a> </li><li> <a href="/article/djgippc.html">SEO的运营效果考核指标</a> </li><li> <a href="/article/djgihoo.html">创新互联LINUX教程:Linux磁盘管理</a> </li> </ul> </div> </div> <footer> <div class="message"> <div class="mess container"> <p>免费获取网站设计制作与品牌网站建设策划方案报价</p> <span>网站设计 + 品牌建站,策划方案 + 精准报价,现在免费领!品牌要升级,官网先发力!免费领取网站设计制作方案 + 精准报价,专业团队帮你落地</span> <form action=""> <input type="text" class="ipt1" placeholder="联系人"> <input type="text" class="ipt2" placeholder="联系电话"> <textarea name="" id="" placeholder="内容描述:描述您的需求,如网站、微信、电商、APP等。"></textarea> <a href="">提交需求</a> </form> </div> </div> <div class="footA"> <div class="footAs container"> <ul> <h3>联系我们</h3> <b>18980820575</b> <li>手机:13518219792</li> <li>地址:湖北孝感市大悟城关镇鄂北物流城13栋125号</li> <li class="hr1"></li> <li>24小时服务热线:18982081108</li> </ul> <ul> <h3>网站建设服务</h3> <li>网页设计</li> <li>网站制作</li> <li>网站开发</li> </ul> <ul> <h3>网站推广服务</h3> <li>营销网站建设</li> <li>百度快速排名</li> <li>整站网站推广</li> </ul> <ul> <h3>网站运维服务</h3> <li>基础维护</li> <li>网站改版</li> <li>网站维护</li> </ul> <ul> <h3>FOLLOW US</h3> <li class="hr2"></li> <li> <dd class="fl"><img src="/Public/Home/img/ewm.png" alt=""><p>微信二维码</p></dd> <dd class="fr"><img src="/Public/Home/img/ewm.png" alt=""><p>微信二维码</p></dd> </li> </ul> </div> <div class="link container"> 友情链接: <a href="http://www.cdcybzsj.com/" title="茶叶包装设计" target="_blank">茶叶包装设计</a>   <a href="http://www.mnhnks.com/" title="湖北摄影器材专卖" target="_blank">湖北摄影器材专卖</a>   <a href="http://www.wjcfa.com/" title="成都家电公司" target="_blank">成都家电公司</a>   <a href="http://www.mscyfdj.com/" title="眉山柴油发电机" target="_blank">眉山柴油发电机</a>   <a href="http://www.sclushan.com/" title="芦山网站制作" target="_blank">芦山网站制作</a>   <a href="http://www.fanghuwan.com/" title="成都边坡防护网" target="_blank">成都边坡防护网</a>   <a href="https://www.cdcxhl.cn/ " title="香港空间" target="_blank">香港空间</a>   <a href="http://www.ysfgg.com/" title="成都标识标牌广告" target="_blank">成都标识标牌广告</a>   <a href="http://www.dmvi.cn/" title="成都广告设计公司" target="_blank">成都广告设计公司</a>   <a href="http://chengdu.cdcxhl.com/dingzhi/" title="定制网站建设" target="_blank">定制网站建设</a>    </div> </div> <div class="footB"> <div class="container"> <div class="fl"> Copyright © 2025 湖北孝感网站建设公司 湖北孝感网站建设公司-选我们!湖北专业的孝感网站制作公司! </div> <div class="fr"> All Rights Reserved 版权所有 <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">鄂ICP备2025125038号-8</a> </div> </div> </div> </footer> </body> </html> <script> $(".con img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); window.onload=function(){ document.oncontextmenu=function(){ return false; } } </script>