Python抓取QQ空间博客文章


外面挂着台风,下午把人人相册的爬虫写了,晚上偶无聊又把QQ空间的博客的爬虫写了,默认只抓取提供的Q号的空间,可以在main.py里面填上Q号,也可以加个循环弄很多个Q号....博客里面的图片就木有理它了,要下载回来也很简单。有空再完善了。

  1. # -*-coding:utf-8-*-   
  2. # Filename: main.py   
  3. # 作者:华亮   
  4. #   
  5.   
  6. from QQ import QQ  
  7.   
  8. if __name__ == '__main__':  
  9.     # 第一个参数为QQ号,第二个为保存文件名   
  10.     QQ.DownloadBlog('414112390''blog.txt')  

 

  1. # -*-coding:utf-8-*-   
  2. # Filename: QQ.py   
  3. # 作者:华亮   
  4. #   
  5.   
  6. import urllib  
  7. import urllib2  
  8. import re  
  9. from HTMLParser import HTMLParser  
  10.   
  11.   
  12. # 获取QQ空间博客列表   
  13. class QQBlogList(HTMLParser):  
  14.     in_key_div = False  
  15.     in_ul = False  
  16.     in_li = False  
  17.     in_a = False  
  18.     blogList = []  
  19.     lasturl = ''  
  20.       
  21.     def handle_starttag(self, tag, attrs):  
  22.         attrs = dict(attrs)  
  23.         if tag == 'div' and 'class' in attrs and attrs['class'] == 'bloglist':  
  24.             self.in_key_div = True  
  25.         elif self.in_key_div:  
  26.             if tag == 'ul':  
  27.                 self.in_ul = True  
  28.             elif self.in_ul and tag == 'li':  
  29.                 self.in_li = True  
  30.             elif self.in_li and tag == 'a' and 'href' in attrs:  
  31.                 self.in_a = True  
  32.                 self.lasturl = attrs['href']  
  33.       
  34.     def handle_data(self, data):  
  35.         if self.in_a:  
  36.             self.blogList.append((data, self.lasturl))  
  37.       
  38.     def handle_endtag(self, tag):  
  39.         if self.in_key_div and tag == 'div':  
  40.             self.in_key_div = False  
  41.         elif self.in_ul and tag == 'ul':  
  42.             self.in_ul = False  
  43.         elif self.in_li and tag == 'li':  
  44.             self.in_li = False  
  45.         elif self.in_a and tag == 'a':  
  46.             self.in_a = False  
  47.               
  48.            
  49.               
  50. class QQ:    
  51.     ''''' 
  52.     QQ 
  53.         作者:华亮 
  54.         说明:自动下载QQ空间博客文章 
  55.     ''' 
  56.          
  57.     @staticmethod        
  58.     def DownloadBlog(qq, filename = None):  
  59.         print 'Start'  
  60.         blogurl = 'http://qz.qq.com/%s/bloglist?page=0' % qq  
  61.         QQ.__Download(blogurl, filename)             
  62.         print 'End' 
  63.      
  64.     @staticmethod  
  65.     def __Download(starturl, filename):  
  66.         url = starturl  
  67.           
  68.         cookieFile = urllib2.HTTPCookieProcessor()  
  69.         opener = urllib2.build_opener(cookieFile)      
  70.           
  71.         # 获取所有页的文章路径   
  72.         while True:  
  73.             req = urllib2.Request(url)  
  74.             result = opener.open(req)          
  75.             text = result.read()       
  76.               
  77.             qq = QQBlogList()          
  78.             qq.feed(text)  
  79.             qq.close()            
  80.                      
  81.             nextpagePattern = re.compile(r'<a href="(.*?)" title="下一页" class="bt_next"><span>下一页</span></a>')                
  82.             nextpage = nextpagePattern.search(text)  
  83.             if nextpage:  
  84.                 url = nextpage.group(1)              
  85.             else:  
  86.                 break    
  87.             
  88.         if not filename:  
  89.             filename = "blog.txt"  
  90.         file = open(filename, 'w')      
  91.           
  92.         # 下载文章   
  93.         blogContentPattern = re.compile(r'<div class="entry_content">(.*?)</div>', re.S)   
  94.         for title, url in qq.blogList:  
  95.             print 'Downloading', title  
  96.             req = urllib2.Request(url)  
  97.             result = opener.open(req)  
  98.             file.write('\n' + title + '\n')  
  99.             ret = blogContentPattern.search( result.read() )  
  100.             if ret:  
  101.                 file.write(ret.group(1).replace('<p>''\n'))  
  102.         file.close()  

相关内容