使用python采集腳本之家電子書資源并自動(dòng)下載到本地的實(shí)例腳本
jb51上面的資源還比較全,就準(zhǔn)備用python來(lái)實(shí)現(xiàn)自動(dòng)采集信息,與下載啦。
Python具有豐富和強(qiáng)大的庫(kù),使用urllib,re等就可以輕松開發(fā)出一個(gè)網(wǎng)絡(luò)信息采集器!
下面,是我寫的一個(gè)實(shí)例腳本,用來(lái)采集某技術(shù)網(wǎng)站的特定欄目的所有電子書資源,并下載到本地保存!
軟件運(yùn)行截圖如下:
在腳本運(yùn)行時(shí)期,不但會(huì)打印出信息到shell窗口,還會(huì)保存日志到txt文件,記錄采集到的頁(yè)面地址,書籍的名稱,大小,服務(wù)器本地下載地址以及百度網(wǎng)盤的下載地址!
實(shí)例采集并下載腳本之家的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() #解決中文亂碼問(wèn)題 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>書籍大?。?lt;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 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 target="_blank">酷云中國(guó)電信下載</a></li> downurl1 = re.search('<li><a href="(?P<downurl1>.+?)" rel="external nofollow" target="_blank">酷云中國(guó)電信下載</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 "該文件已經(jīng)下載了!"; 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不存在!文件沒(méi)有自動(dòng)下載!r"); file_error.write('r'); #<li><a rel="external nofollow" target="_blank">百度網(wǎng)盤下載2</a></li> downurl2 = re.search('</a></li><li><a href="(?P<downurl2>.+?)" rel="external nofollow" target="_blank">百度網(wǎng)盤下載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 = "http://www.dbjr.com.cn"+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 = "http://www.dbjr.com.cn/ books/list476_%d.html"%pagevalue; print listurl; file_object.write(listurl+'r'); getBooksUrl(listurl); file_object.close(); file_error.close();
注意,上面代碼部分地方的url被我換了。
總結(jié)
以上所述是小編給大家介紹的python采集jb51電子書資源并自動(dòng)下載到本地實(shí)例腳本,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Django實(shí)現(xiàn)列表頁(yè)商品數(shù)據(jù)返回教程
這篇文章主要介紹了Django實(shí)現(xiàn)列表頁(yè)商品數(shù)據(jù)返回教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Python numpy實(shí)現(xiàn)數(shù)組合并實(shí)例(vstack,hstack)
這篇文章主要介紹了Python numpy實(shí)現(xiàn)數(shù)組合并(官網(wǎng)實(shí)例),涉及vstack,hstack的簡(jiǎn)單用法,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01python檢測(cè)遠(yuǎn)程服務(wù)器tcp端口的方法
這篇文章主要介紹了python檢測(cè)遠(yuǎn)程服務(wù)器tcp端口的方法,涉及Python操作socket檢測(cè)tcp端口的技巧,需要的朋友可以參考下2015-03-03pytest實(shí)現(xiàn)多進(jìn)程與多線程運(yùn)行超好用的插件
本文主要介紹了pytest實(shí)現(xiàn)多進(jìn)程與多線程運(yùn)行超好用的插件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Python使用pyyaml模塊處理yaml數(shù)據(jù)
這篇文章主要介紹了Python使用pyyaml模塊處理yaml數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04NVIDIA安裝CUDA的實(shí)現(xiàn)(圖文教程)
本文主要介紹了NVIDIA安裝CUDA的實(shí)現(xiàn),包括系統(tǒng)要求、軟件下載、安裝步驟以及常見(jiàn)問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01詳解Python如何利用pymysql封裝項(xiàng)目通用的連接和查詢
一個(gè)項(xiàng)目通常都需要有數(shù)據(jù)庫(kù),本文就來(lái)為大家詳細(xì)講講Python如何利用pymysql簡(jiǎn)單分裝一個(gè)通用的連接,關(guān)閉和查詢,需要的可以參考一下2022-07-07