[Python學習] 簡單爬取CSDN下載資源資訊

來源:互聯網
上載者:User

標籤:python   爬蟲   擷取csdn資訊   基礎知識   

        這是一篇Python爬取CSDN下載資源資訊的例子,主要是通過urllib2擷取CSDN某個人所有資源的資源URL、資源名稱、下載次數、分數等資訊;寫這篇文章的原因是我想擷取自己的資源所有的評論資訊,但是由於評論採用JS臨時載入,所以這篇文章先簡單介紹如何人工分析HTML頁面爬取資訊。

原始碼
# coding=utf-8  import urllib  import time  import re  import os#************************************************** #第一步 遍曆擷取每頁對應主題的URL #http://download.csdn.net/user/eastmount/uploads/1#http://download.csdn.net/user/eastmount/uploads/8#**************************************************num=1 #記錄資源總數 共46個資源number=1 #記錄列表總數1-8fileurl=open('csdn_url.txt','w+')  fileurl.write('****************擷取資源URL*************\n\n')while number<9:    url='http://download.csdn.net/user/eastmount/uploads/' + str(number)    fileurl.write('下載列表URL:'+url+'\n\n')    print unicode('下載列表URL:'+url,'utf-8')    content=urllib.urlopen(url).read()    open('csdn.html','w+').write(content)    #擷取包含URL塊內容 匹配需要計算</div>個數    start=content.find(r'<div class="list-container mb-bg">')      end=content.find(r'<div class="page_nav">')    cutcontent=content[start:end]    #print cutcontent    #擷取塊內容中URL    #形如<dt><div><img 表徵圖></div><h3><a href>標題</a></h3></dt>    res_dt = r'<dt>(.*?)</dt>'      m_dt =  re.findall(res_dt,cutcontent,re.S|re.M)      for obj in m_dt:        #記錄URL數量        print '******************************************'        print '第'+str(num)+'個資源'        fileurl.write('******************************************\n')        fileurl.write('第'+str(num)+'個資源\n')        num = num +1        #擷取具體URL        url_list = re.findall(r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')", obj)        for url in url_list:            url_load='http://download.csdn.net'+url            print 'URL: '+url_load            fileurl.write('URL: http://download.csdn.net'+url+'\n')        #擷取資源標題        #<a href="/detail/eastmount/8757243">MFC顯示BMP圖片</a>        res_title = r'<a href=.*?>(.*?)</a>'        title = re.findall(res_title,obj,re.S|re.M)        for t in title:            print unicode('Title: ' + t,'utf-8')              fileurl.write('Title: ' + t +'\n')        #**************************************************         #第二步 遍曆具體資源的內容及評論         #http://download.csdn.net/detail/eastmount/8785591        #**************************************************        #定位指定結構化資訊盒Infobox        resources = urllib.urlopen(url_load).read()        open('resource.html','w+').write(resources)        start_res=resources.find(r'<div class="wraper-info">')          end_res=resources.find(r'<div class="enter-link">')        infobox=resources[start_res:end_res]        #擷取資源積分、下載次數、資源類型、資源大小(前4個<span></span>)        res_span = r'<span>(.*?)</span>'          m_span = re.findall(res_span,infobox,re.S|re.M)        print '資源積分: '+m_span[0]        fileurl.write('資源積分: ' + m_span[0] +'\n')        print '下載次數: '+m_span[1]        fileurl.write('下載次數: ' + m_span[1] +'\n')        print '資源類型: '+m_span[2]        fileurl.write('資源類型: ' + m_span[2] +'\n')        print '資源大小: '+m_span[3]        fileurl.write('資源大小: ' + m_span[3] +'\n')        #**************************************************        #第三步 如何擷取評論        #http://jeanphix.me/Ghost.py/        #http://segmentfault.com/q/1010000000143340        #http://casperjs.org/        #**************************************************         else:        fileurl.write('******************************************\n\n')        print '******************************************\n'        print 'Load Next List\n'        number = number+1 #列表加1#退出所有迴圈else:    fileurl.close()

顯示結果
        顯示內容包括資源URL、資源標題、資源積分、下載次數、資源類型和資源大小:

        比如現在爬取郭霖大神的資源資訊,其中頁面連結如下:(共7頁)
              http://download.csdn.net/user/sinyu890807/uploads/1
              http://download.csdn.net/user/sinyu890807/uploads/7
        簡單修改Python原始碼URL後,下載頁面如所示:


        運行結果如所示:



HTML分析
        首先,擷取每列中的所有資源的URL和標題,通過分析原始碼。
<dt>   <div class="icon"><img src="/images/minetype/rar.gif" title="rar檔案"></div>   <div class="btns"></div>     <h3><a href="/detail/eastmount/8772951">          MFC 影像處理之幾何運算 映像平移旋轉縮放鏡像(源碼)</a>       <span class="points">0</span>   </h3></dt>           <dd class="meta">上傳者:    <a class="user_name" href="/user/eastmount">eastmount</a>         | 上傳時間:2015-06-04         | 下載26次</dd><dd class="intro">        該資源主要參考我的部落格【數位影像處理】六.MFC空間幾何變換之映像平移、鏡像、旋轉        縮放詳解,主要講述基於VC++6.0 MFC影像處理的應用知識,要通過MFC單文檔視圖實現顯        示BMP圖片。</dd><dd class="tag">     <a href="/tag/MFC">MFC</a>     <a href="/tag/%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86">影像處理</a><</dd>
        對應的HTML顯示如所示:


        然後通過URL去到具體的資源擷取我自己稱為像訊息盒的資訊:

        對應審查元素的資訊如下所示,擷取<span>0分</span>即可:

        最後我想做的事擷取評論資訊,但是它是通過JS實現的:
<div class="section-list panel panel-default">   <div class="panel-heading">      <h3 class="panel-title">資源評論</h3>   </div>   <!-- recommand -->   <script language='JavaScript' defer type='text/javascript'         src='/js/comment.js'></script>   <div class="recommand download_comment panel-body" sourceid="8772951"></div></div>
        顯示的JS頁面部分如下:
var base_url= (window.location.host.substring(0,5)=='local') ? 'http://local.downloadv3.csdn.net' : 'http://download.csdn.net';base_url = "";$(document).ready(function(){CC_Comment.initConfig();CC_Comment.getContent(1);});var CC_Comment = {sourceid:0,initConfig:function(){var sid = parseInt($(".download_comment").attr('sourceid'));if(isNaN(sid) || sid<=0){this.sourceid = 0;}else{this.sourceid = sid;}}....}

        最後希望文章對你有所協助吧!下一篇準備分析下Python如何擷取JS的評論資訊,同時該篇文章可以給你提供一種簡單的人工分析頁面的例子;也可以擷取某個人CSDN資源下載多、分數高的給你挑選。基礎知識,僅供參考~
      (By:Eastmount 2015-7-21 下午5點   http://blog.csdn.net/eastmount/)


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

[Python學習] 簡單爬取CSDN下載資源資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.