本篇文章的內容是如何用Python爬蟲獲取那些價值博文,現在分享給大家,有需要的朋友可以參考一下這篇文章地的內容
作者 CDA資料分析師
#在CSDN上有許多精彩的技術部落格文章,我們可以把它爬取下來,保存在本地磁盤,可以很方便以後閱讀和學習,現在我們就用python編寫一段爬蟲代碼,來實現這個目的。
我們想要做的事情:自動讀取部落格文章,記錄標題,把心儀的文章儲存到個人電腦硬碟裡供日後學習參考。
過程大體分成以下幾個步驟:
#1. 找到爬取的目標網址;
2. 分析網頁,找到自已想要保存的信息,這裡我們主要保存是博客的文章內容;
#3. 清洗整理爬取下來的信息,保存在本地磁盤。
#開啟csdn的網頁,作為一個範例,我們隨機開啟一個網頁:
http://blog.csdn.net/u013088062/article/list/1。
可以看到,部落客對《C++卷積神經網路》和它有關機電腦的文章都寫得不錯。
爬蟲程式碼依想法分為三個類別(class),下面3個帶有「#」的分別給出了每一個類別的開頭(具體程式碼附後,供大家實際運行實作):
採用「類別(class)」的方式屬於Python的物件導向編程,在某些時候比我們通常使用的面向過程的編程方便,在大型工程中經常使用物件導向程式設計。對於初學者來說,物件導向程式設計不易掌握,但是經過學習習慣之後,會逐步慢慢從過程導向到物件導向程式設計過渡。
特別注意的是,RePage類別主要用正規表示式處理從網頁中取得的信息,而正規表示式設定字串樣式如下:
用正規表示式去匹配所要爬取的內容,用Python和其它軟體工具都可以實現。正規表示式有許多規則,各個軟體使用起來大同小異。用好正規表示式是爬蟲和文字挖掘的一個重要內容。
############SaveText類別則是把資訊保存在本地,效果如下:########## ## ###################
##用python寫爬蟲程式碼,簡潔又有效率。這篇文章僅從爬蟲最基本的用法做了講解,有興趣的朋友可以下載代碼看看,希望大家從中有收穫。
附相關Python程式碼:
<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> 1</span><span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">#-*-coding:UTF-8-*-</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> 2</span><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">import</span> re<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> 3</span><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">import</span> urllib2<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> 4</span><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">import</span> sys<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> 5</span><span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">##目的:讀取部落格文章,記錄標題,用Htnl格式儲存記憶體內容</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> 6</span><span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">#版本:python2.7.13</span><br>## 7<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>#功能:讀取網頁內容<span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span><br> 8<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;color:inherit;line-height:inherit;">class<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"> </span>GetHtmlPage<span style="font-size:inherit;line-height:inherit;color:rgb(239,239,143);"></span>()<span style="font-size:inherit;color:inherit;line-height:inherit;">: </span></span><br> 9<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> </span>#注意大小寫<span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span><br>10<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> </span><span style="font-size:inherit;color:inherit;line-height:inherit;">def<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"> </span>__init__<span style="font-size:inherit;line-height:inherit;color:rgb(239,239,143);">#__init__</span><span style="font-size:inherit;color:inherit;line-height:inherit;">#(self,strPage)</span>:</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">11</span> self.strPapge = strPage<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">#12</span> # c ##of <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">##13</span> <br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">def</span><span style="font-size:inherit;color:inherit;line-height:inherit;">#GetPage<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span>(self)<span style="font-size:inherit;line-height:inherit;color:rgb(239,239,143);">:</span><span style="font-size:inherit;color:inherit;line-height:inherit;"></span>14</span> <br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">#14</span> <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span>14<br> = urllib2.Request(self.strPapge) <span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"># 建立頁面請求</span><span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"></span>15<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"> </span># 建立頁面要求<br>######15### 1 "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0"#########################################################################################################################################################################################################################)<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">16</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">try</span>:<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">17</span> <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">網頁請求</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">18</span> page = cn.read() uPage = page.decode(<span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">"utf-8"</span>) <br>#網頁編碼<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">20</span> cn.close()<span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span>21<br> # <span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">21</span> 時# <br>except<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> urllib2.URLError, e: </span>#擷取例外圖<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span><br>23<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> # print ##o# #, e. code</span><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">24</span> <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">return</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">25</span> o <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">#捕捉例外圖</span> <span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"></span>26<br> <span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">print</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">'HTTP Error:'</span> + .reason return<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>28<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"> </span>return<span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"> rePage</span><br>29<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>##正規表示式,取得想要的內容<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span>##<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">30</span><span style="font-size:inherit;color:inherit;line-height:inherit;"><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">class</span> <span style="font-size:inherit;line-height:inherit;color:rgb(239,239,143);">RePage</span><span style="font-size:inherit;color:inherit;line-height:inherit;">()</span>:</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">31</span><span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">#正規表示式擷取內容,返回鍊錶</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">32</span> <span style="font-size:inherit;color:inherit;line-height:inherit;"><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">def</span> <span style="font-size:inherit;line-height:inherit;color:rgb(239,239,143);">GetReText</span><span style="font-size:inherit;color:inherit;line-height:inherit;">(self,page,recode)</span>:</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">33</span> rePage = re.findall(recode,page,re.S)<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">34</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">return</span> rePage<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> </span>35<span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span>#儲存文字<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>36<span style="font-size:inherit;color:inherit;line-height:inherit;"><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span>#class<span style="font-size:inherit;line-height:inherit;color:rgb(239,239,143);"> </span>SaveText<span style="font-size:inherit;color:inherit;line-height:inherit;"></span>()</span>:<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>37<span style="font-size:inherit;color:inherit;line-height:inherit;"> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span>def<span style="font-size:inherit;line-height:inherit;color:rgb(239,239,143);"></span>#Save<span style="font-size:inherit;color:inherit;line-height:inherit;"></span>(self,text,tilte)</span>:<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>38<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"> </span>try<br>:<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>39<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"> t=</span>"blog\\"<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">+tilte+</span>".html"<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>40<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"> f = file(t,</span>"a"<br>)<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>41<br> f.write(text)<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">## 42</span> f.close()<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">43</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">except</span> IOError,e:<br>## 44## <span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>45<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span>if<br> __name__ == <span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">"__main__"</span>:<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">46</span> s = SaveText()<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">47</span> <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">#檔案編碼</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">48</span># <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">##字元正確解碼</span> <br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">#49</span> reload(sys)<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">50</span> sys.setdefaultencoding( <span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">"utf-8"</span> ) <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">#取得系統的預設編碼</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">51</span> <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"># 取得網頁</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">52</span> page = GetHtmlPage(<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">"http://blog.csdn.net/ u013088062/article/list/1"</span>)<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">53</span> htmlPage = page.GetPage()<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">#54</span> <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>54<br> <span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"><span class="link_title"></span></span><span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"></span><span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;line-height:inherit;color:rgb(140,208,211);"></span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;line-height:inherit;color:rgb(140,208,211);"></span><span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"><span style="color:#FF0000;">#。 </span>#55</span> reServer = RePage()<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"></span>56<span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"> reBlog = reServer.GetReText(htmlPage,</span>r'<br>.*?(\s.+?)<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">'</span>) <span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">#取得網址連結與標題</span><span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"></span>57<br> ####再向下取得文字#########58## # ###for### ref ###in### reBlog:#######59### pageHeard = ###"http://blog.csdn.net/"### ## ##加上連結頭#########60### strPage = pageHeard+ref[###0###]######61### tilte=ref[#####61### tilte=ref[#####1 ###].replace(###'###[置頂]###'###, ###""###) ####用替換的功能去除雜的英文##### #####62### tilte=tilte.replace(###"\r\n"###,###""###).lstrip().rstrip()###<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">63</span> <span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);">##取得文字</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">64</span> htmlPage = GetHtmlPage(strPage)<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">65age#Phtml </span>##66<br> reBlogText = reServer.GetReText(htmlPageData,<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">'</span><span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"></span></span>
<br>#(.+?)<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;line-height:inherit;color:rgb(127,159,127);"></span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span>#(.+?)<span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);"></span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);"></span><br>#(.+?)<span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"></span><br>
#(.+?)
')67 #儲存檔案
inreBlogText :69 s1='\n'
+s1
70
以上是如何用Python爬蟲取得那些價值博文的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器