這是一篇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-8
fileurl=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/1)
[http://download.csdn.net/user/sinyu890807/uploads/7](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/](http://blog.csdn.net/eastmount/))
- 前言
- [Python學習] 專題一.函數的基礎知識
- [Python學習] 專題二.條件語句和循環語句的基礎知識
- [Python學習] 專題三.字符串的基礎知識
- [Python學習] 簡單網絡爬蟲抓取博客文章及思想介紹
- [Python學習] 專題四.文件基礎知識
- [python學習] 簡單爬取維基百科程序語言消息盒
- [python學習] 簡單爬取圖片網站圖庫中圖片
- [python知識] 爬蟲知識之BeautifulSoup庫安裝及簡單介紹
- [python+nltk] 自然語言處理簡單介紹和NLTK壞境配置及入門知識(一)
- [python學習] 模仿瀏覽器下載CSDN源文并實現PDF格式備份
- [Python學習] 簡單爬取CSDN下載資源信息
- [Python] 專題五.列表基礎知識 二維list排序、獲取下標和處理txt文本實例
- [Python學習] 專題六.局部變量、全局變量global、導入模塊變量
- [python] 專題七.網絡編程之套接字Socket、TCP和UDP通信實例
- [python] 專題八.多線程編程之thread和threading