本篇文章的内容是如何用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><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;"> 7</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;"> 8</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);">GetHtmlPage</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;"> 9</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;">10</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);">__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> <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;">13</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);">GetPage</span><span style="font-size:inherit;color:inherit;line-height:inherit;">(self)</span>:</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">14</span> req = urllib2.Request(self.strPapge) <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;">15</span> rep = req.add_header(<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">"User-Agent"</span>,<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">"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>)<br><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> cn = urllib2.urlopen(req) <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() <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;">19</span> uPage = page.decode(<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;">20</span> cn.close()<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">21</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">return</span> uPage<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">22</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">except</span> urllib2.URLError, e: <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;">23</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">print</span> <span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">'URLError:'</span>, e.code<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">24</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">return</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">25</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">except</span> urllib2.HTTPError, e: <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;">26</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">print</span> <span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">'HTTP Error:'</span> + e.reason<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">27</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">return</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">28</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;">29</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;">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;">35</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;">36</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);">SaveText</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;">37</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);">Save</span><span style="font-size:inherit;color:inherit;line-height:inherit;">(self,text,tilte)</span>:</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">38</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;">39</span> t=<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">"blog\\"</span>+tilte+<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">".html"</span><br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">40</span> f = file(t,<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">"a"</span>)<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">41</span> f.write(text)<br><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><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">44</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">print</span> e.message<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">45</span><span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">if</span> __name__ == <span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">"__main__"</span>:<br><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;">55</span> reServer = RePage()<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">56</span> reBlog = reServer.GetReText(htmlPage,<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">r'<span class="link_title">.*?(\s.+?)</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;">57</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;">58</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">for</span> ref <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">in</span> reBlog:<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">59</span> pageHeard = <span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">"http://blog.csdn.net/"</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;">60</span> strPage = pageHeard+ref[<span style="font-size:inherit;line-height:inherit;color:rgb(140,208,211);">0</span>]<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">61</span> tilte=ref[<span style="font-size:inherit;line-height:inherit;color:rgb(140,208,211);">1</span>].replace(<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">'<span style="color:#FF0000;">[置顶]</span>'</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;">62</span> tilte=tilte.replace(<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">"\r\n"</span>,<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">""</span>).lstrip().rstrip()<br><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;">65</span> htmlPageData = htmlPage.GetPage()<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">66</span> reBlogText = reServer.GetReText(htmlPageData,<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;">67</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;">68</span> <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">for</span> s1 <span style="font-size:inherit;line-height:inherit;color:rgb(227,206,171);">in</span> reBlogText:<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">69</span> s1=<span style="font-size:inherit;line-height:inherit;color:rgb(204,147,147);">'\n'</span>+s1<br><span style="padding-right:20px;font-size:inherit;line-height:inherit;color:rgb(140,208,211);word-spacing:0px;">70</span> s.Save(s1,tilte)<br>
相关推荐:
以上是如何用Python爬虫获取那些价值博文的详细内容。更多信息请关注PHP中文网其他相关文章!