最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用python采集腳本之家電子書資源并自動下載到本地的實(shí)例腳本

 更新時間:2018年10月23日 15:58:26   作者:忘憂草論壇  
這篇文章主要介紹了python采集jb51電子書資源并自動下載到本地實(shí)例教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

jb51上面的資源還比較全,就準(zhǔn)備用python來實(shí)現(xiàn)自動采集信息,與下載啦。

Python具有豐富和強(qiáng)大的庫,使用urllib,re等就可以輕松開發(fā)出一個網(wǎng)絡(luò)信息采集器!

下面,是我寫的一個實(shí)例腳本,用來采集某技術(shù)網(wǎng)站的特定欄目的所有電子書資源,并下載到本地保存!

軟件運(yùn)行截圖如下:

在腳本運(yùn)行時期,不但會打印出信息到shell窗口,還會保存日志到txt文件,記錄采集到的頁面地址,書籍的名稱,大小,服務(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()
 #解決中文亂碼問題
 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">酷云中國電信下載</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 "該文件已經(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不存在!文件沒有自動下載!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://m.fzitv.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 = "http://m.fzitv.net/ books/list476_%d.html"%pagevalue;
 print listurl;
 file_object.write(listurl+'r');
 getBooksUrl(listurl);
 file_object.close();
 file_error.close();

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

總結(jié)

以上所述是小編給大家介紹的python采集jb51電子書資源并自動下載到本地實(shí)例腳本,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Python 創(chuàng)建TCP服務(wù)器的方法

    Python 創(chuàng)建TCP服務(wù)器的方法

    這篇文章主要介紹了Python 創(chuàng)建TCP服務(wù)器的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • python解壓zip包中文亂碼解決方法

    python解壓zip包中文亂碼解決方法

    這篇文章主要介紹了python解壓zip包中文亂碼解決方法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11
  • Python模擬登錄網(wǎng)易云音樂并自動簽到

    Python模擬登錄網(wǎng)易云音樂并自動簽到

    時隔三周沒有和大家見過面了,最近在研究python模擬登陸專題,話不多說,讓我們愉快地開始實(shí)現(xiàn)模擬登陸實(shí)現(xiàn)網(wǎng)易云自動簽到,需要的朋友可以參考下
    2021-06-06
  • Jupyter Lab設(shè)置切換虛擬環(huán)境的實(shí)現(xiàn)步驟

    Jupyter Lab設(shè)置切換虛擬環(huán)境的實(shí)現(xiàn)步驟

    本文主要介紹了Jupyter Lab設(shè)置切換虛擬環(huán)境的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • python爬蟲獲取新浪新聞教學(xué)

    python爬蟲獲取新浪新聞教學(xué)

    在本篇內(nèi)容中小編給大家分享的是關(guān)于python爬蟲獲取新浪新聞的相關(guān)步驟和知識點(diǎn),需要的可以跟著學(xué)習(xí)下。
    2018-12-12
  • Jupyter中markdown的操作方法

    Jupyter中markdown的操作方法

    Jupyter Notebook是基于網(wǎng)頁的用于交互計(jì)算的應(yīng)用程序,Jupyter notebook,作為Python廣受歡迎的一款I(lǐng)DLE,其直觀性、簡易性、易于閱讀等優(yōu)點(diǎn)廣受許多Python用戶所推薦,這篇文章介紹Jupyter中markdown的操作,感興趣的朋友一起看看吧
    2024-01-01
  • Python中實(shí)現(xiàn)定時任務(wù)常見的幾種方式

    Python中實(shí)現(xiàn)定時任務(wù)常見的幾種方式

    在Python中,實(shí)現(xiàn)定時任務(wù)是一個常見的需求,無論是在自動化腳本、數(shù)據(jù)處理、系統(tǒng)監(jiān)控還是其他許多應(yīng)用場景中,Python提供了多種方法來實(shí)現(xiàn)定時任務(wù),包括使用標(biāo)準(zhǔn)庫、第三方庫以及系統(tǒng)級別的工具,本文將詳細(xì)介紹幾種常見的Python定時任務(wù)實(shí)現(xiàn)方式
    2024-08-08
  • Python實(shí)現(xiàn)的序列化和反序列化二叉樹算法示例

    Python實(shí)現(xiàn)的序列化和反序列化二叉樹算法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的序列化和反序列化二叉樹算法,結(jié)合實(shí)例形式分析了Python二叉樹的構(gòu)造、遍歷、序列化、反序列化等相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • Python函數(shù)基礎(chǔ)

    Python函數(shù)基礎(chǔ)

    這篇文章主要從函數(shù)開始介紹展開Python函數(shù),以最基本的函數(shù)定義方法描述,需要的朋友可以參考下文簡單的介紹
    2021-08-08
  • EM算法的python實(shí)現(xiàn)的方法步驟

    EM算法的python實(shí)現(xiàn)的方法步驟

    本篇文章主要介紹了EM算法的python實(shí)現(xiàn)的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01

最新評論

乐都县| 三都| 磐石市| 临西县| 图木舒克市| 重庆市| 崇州市| 彭泽县| 顺昌县| 惠来县| 平潭县| 武陟县| 岳池县| 阿鲁科尔沁旗| 高尔夫| 道孚县| 兰考县| 车险| 珲春市| 铁力市| 黄石市| 林西县| 彭州市| 吉首市| 榕江县| 玛纳斯县| 绍兴市| 民乐县| 将乐县| 平塘县| 睢宁县| 万州区| 景宁| 溆浦县| 蒙自县| 沭阳县| 益阳市| 临夏市| 原平市| 南汇区| 醴陵市|