python新浪博客爬虫(纯自己写)

python新浪博客爬虫(纯自己写)

一直在学习javaweb方面的知识,什么J2SE,ssh框架,tomcat,学的累了。突然想学习一门脚本语言,虽然js也会点,就是不怎么熟练。感觉Python挺有意思的,就花了两天学习了下,还知道可以比较方便的用来编写网络爬虫,觉得很有意思,所以就觉得自己编写一个,花了一晚上时间写了一个从新浪博客上爬韩寒文章的爬虫。自己编写的,洗的不好的地方,请指出,大家一起讨论。 一直在学习javaweb方面的知识,什么J2SE,ssh框架,tomcat,学的累了。突然想学习一门脚本语言,虽然js也会点,就是不怎么熟练。感觉Python挺有意思的,就花了两天学习了下,还知道可以比较方便的用来编写网络爬虫,觉得很有意思,所以就觉得自己编写一个,花了一晚上时间写了一个从新浪博客上爬韩寒文章的爬虫。自己编写的,洗的不好的地方,请指出,大家一起讨论。
直接贴出代码,每个函数上面都是注释,很容易看懂的。 直接贴出代码,每个函数上面都是注释,很容易看懂的。
还有一个工具类,主要是除去文章中多余的链接,换行之类的 还有一个工具类,主要是除去文章中多余的链接,换行之类的
__author__=sw                                                                                                                              
 # -*- coding:utf-8 -*-
 import re
 
 class Tool:
     removeImg = re.compile(<img.*?>| {1,7}| )
     removeAddr = re.compile(<a.*?>|</a>)
     replaceLine = re.compile(<tr>|<div>|</div>|</p>)
     replaceBR = re.compile(<br><br>|<br>)
     removeExtraTag = re.compile(<.*?>)
     removeNoneLine = re.compile(
+)
 
     def replace(self,x):
         x = re.sub(self.removeImg,"",x)
         x = re.sub(self.removeAddr,"",x)
         x = re.sub(self.replaceLine,"
",x)
         x = re.sub(self.replaceBR,"
",x)
         x = re.sub(self.removeExtraTag,"",x)
         x = re.sub(self.removeNoneLine,"
",x)
         return x.strip()
__author__=sw # -*- coding:utf-8 -*- import re class Tool: removeImg = re.compile( | {1,7}| ) removeAddr = re.compile( |) replaceLine = re.compile(|
|
|

) replaceBR = re.compile(

|
) removeExtraTag = re.compile(<.*?>) removeNoneLine = re.compile( +) def replace(self,x): x = re.sub(self.removeImg,"",x) x = re.sub(self.removeAddr,"",x) x = re.sub(self.replaceLine," ",x) x = re.sub(self.replaceBR," ",x) x = re.sub(self.removeExtraTag,"",x) x = re.sub(self.removeNoneLine," ",x) return x.strip()
事实上有的地方还是可以改进的,现在只是爬取韩寒博客的文章,下一个版本可以输入一个博客的网址,从输入的网址中爬取文章,有时间再改进改进吧。 事实上有的地方还是可以改进的,现在只是爬取韩寒博客的文章,下一个版本可以输入一个博客的网址,从输入的网址中爬取文章,有时间再改进改进吧。
经验分享 程序员 微信小程序 职场和发展