python 抓取分析 SGMLParser 实例

简介:
数据:
希望 抓取
div > p id='da' > a text
和 div > p id='da' > html
< div >  
   
< id ="tt" >
     
< href =/tag/php > no no </ a >  
  
</ p >
  
< id ='da' >
    
< href =/tag/php > php </ a >  
    
< href =/tag/python > python </ a >  
    
< href =/tag/vim > vim </ a >  
    
< href =/tag/windows > windows </ a >  
   
< href =/tag/wingide > wingide </ a >  
  
</ p >  
</ div >  
< id ='da' >
   
< href =/tag/wingide > hehe </ a >  
</ p >  
希望结果为 
$ python t.py
a_text: ["'php'", "'python'", "'vim'", "'windows'", "'wingide'"]

 div_html[0]:
 
< id ="da" >
    
< href ="/tag/php" > php </ a >
    
< href ="/tag/python" > python </ a >
    
< href ="/tag/vim" > vim </ a >
    
< href ="/tag/windows" > windows </ a >
    < href ="/tag/wingide" >刘凯毅 </ a >
  
</ p >
#说明
其实 SGMLParser 我感觉最关键的是 

# /usr/lib/python2.5/sgmllib.py
#
 Internal -- finish parsing of <tag/data/ (same as <tag>data</tag>)
     def  finish_shorttag(self, tag, data):
        
# 而 finish_starttag finish_endtag 抓取会调用 end_* start_* 什么的 
        self.finish_starttag(tag, []) 
        self.handle_data(data)
        self.finish_endtag(tag)
代码:
# !python
#
coding=UTF-8

from  sgmllib  import  SGMLParser
class  TestSGMLParser(SGMLParser):

    
def  reset(self, verbose = 0):
        SGMLParser.reset(self)
        
        
# 提取 a text ; div html 
        self.a_text = []
        self.div_html
= []

        
# 寄存变量
        self.data_text  =   ""
        self.data_html 
=   ""
        
        
# 业务逻辑表示变量
         # 抓取 div > p id="da" > a
         # 由于需要得到div p 的 html > test_div_p = 0 , 1 , 2
        self.test_div = False
        self.test_div_p
= 0
        self.test_div_p_a
= False
        

    
#  重写 handle_data 
     #  寄存变量 填充值
     def  handle_data(self, data):
            self.data_text 
=  self.data_text  +  data
        
if  self.test_div_p :
            self.data_html 
=  self.data_html  + data

            
    
#  重写 finish_starttag 
     #  self.data_html 填充值
     def  finish_starttag(self, tag, attrs):
        SGMLParser.finish_starttag(self, tag, attrs)
        
if  self.test_div_p :
                strattrs 
=   "" .join([ '  %s="%s" '   %  (key, value)  for  key, value  in  attrs])
                self.data_html
= self.data_html + " <%(tag)s%(strattrs)s> "   %  locals()
    
    
#  重写 finish_endtag 
     #  self.data_html 填充值
         def  finish_endtag(self, tag):         
        SGMLParser.finish_endtag(self, tag)
        
if  self.test_div_p  ==   2  :
            self.data_html
= self.data_html + " </%(tag)s> "   %  locals()
        
elif  self.test_div_p  ==   1  :
            self.data_html
= self.data_html + " </%(tag)s> "   %  locals()
            self.test_div_p 
=  0


    
#  self.test_div 状态修改
         def  start_div(self, attrs):
        self.test_div
= True

    
#  self.test_div 状态修改
     #  self.div_html 填充
         def  end_div(self):
        
if  self.test_div :    
            self.div_html.append(self.data_html)
        self.test_div
= False

    
#  self.test_div_p 状态修改 2 为可以填充
         def  start_p(self, attrs):
            
if  self.test_div  and  attrs  and   ' id '   in  [ key  for  key, value  in  attrs ]  and   len([ value  for  key, value  in  attrs  if  key == ' id '   and  value == ' da ' ]) > 0 :
            self.test_div_p
= 2

    
#  self.test_div_p 状态修改 1 为只能填充最后一次
         def  end_p(self):
        
if  self.test_div_p  ==   2  :
            self.test_div_p
= 1
    
        
#  self.test_div_p_a 状态修改
         def  start_a(self, attrs):
        self.data_text 
=   ""
            
if  self.test_div_p :
                self.test_div_p_a
= True
    
        
#  self.test_div_p_a 状态修改
     #  self.a_text 填充
         def  end_a(self):
            
if  self.test_div_p  and  self.test_div  and  self.test_div_p_a  :
                    self.a_text.append(repr(self.data_text))
            self.test_div_p_a
= False

        
def  close(self):
            SGMLParser.close(self)


if   __name__   ==   ' __main__ ' :
    
try :
        f 
=  open( ' google.html ' ' r ' )
        data 
=  f.read()
        x
= TestSGMLParser()
        x.feed(data)
        x.close()
        
#  我这 gvim utf8 ; cygwin gbk ,转码  unicode( str , 'utf8').encode('gbk')
         print   " a_text: %s \n div_html[0]: \n %s " % (x.a_text[:-1],  unicode(x.div_html[0],  ' utf8 ' ).encode( ' gbk ' ) )

    
except  IOError, msg:
        
print  file,  " : " , msg

    


页面抓取
抓取 pycurl + 分析用  SGMLParser + 验证码用 pytesser 

下面就差算法了,抓取的准备工作终于要完成了。

本文转自博客园刘凯毅的博客,原文链接:python 抓取分析 SGMLParser 实例,如需转载请自行联系原博主。

目录
相关文章
|
3天前
|
机器学习/深度学习 算法 数据挖掘
PYTHON银行机器学习:回归、随机森林、KNN近邻、决策树、高斯朴素贝叶斯、支持向量机SVM分析营销活动数据|数据分享-2
PYTHON银行机器学习:回归、随机森林、KNN近邻、决策树、高斯朴素贝叶斯、支持向量机SVM分析营销活动数据|数据分享
24 1
|
2天前
|
机器学习/深度学习 算法 vr&ar
PYTHON用时变马尔可夫区制转换(MARKOV REGIME SWITCHING)自回归模型分析经济时间序列
PYTHON用时变马尔可夫区制转换(MARKOV REGIME SWITCHING)自回归模型分析经济时间序列
13 4
|
2天前
|
机器学习/深度学习 算法 Python
数据分享|Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
数据分享|Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
20 4
|
2天前
|
API vr&ar Python
Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列(上)
Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列
30 5
|
8天前
|
vr&ar Python
Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列4
Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列
33 0
|
8天前
|
存储 机器学习/深度学习 数据可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
56 6
|
8天前
|
机器学习/深度学习 算法 数据可视化
python用支持向量机回归(SVR)模型分析用电量预测电力消费
python用支持向量机回归(SVR)模型分析用电量预测电力消费
33 7
|
8天前
|
机器学习/深度学习 数据可视化 算法
PYTHON用决策树分类预测糖尿病和可视化实例
PYTHON用决策树分类预测糖尿病和可视化实例
16 0
|
机器学习/深度学习 算法 Python
Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
23 0
|
8天前
|
算法 数据可视化 Python
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
13 0