设为首页 收藏本站
查看: 845|回复: 0

[经验分享] 用python解析html

[复制链接]

尚未签到

发表于 2015-4-24 07:08:07 | 显示全部楼层 |阅读模式
  http://blog.iyunv.com/adrianfeng/article/details/5881850
  
  python中,有三个库可以解析html文本,HTMLParser,sgmllib,htmllib。他们的实现方法不通,但功能差不多。这三个库中提供解析html的类都是基类,本身并不做具体的工作。他们在发现的元件后(如标签、注释、声名等),会调用相应的函数,这些函数必须重载,因为基类中不作处理。
  
  比如:
  """Advice
The IETF admonishes:
Be strict in what you send.

  

"""
  
  如果对这个数据做处理,当检测到标签时,对于HTMLParser,会调用handle_starttag函数。
  
  
  下面具体介绍下几个库
1、HTMLParser




[python] view plaincopy

  • #------------------ HTMLParser_stack.py ------------------#
  • #-- coding: GBK --
  • import HTMLParser,sys,os,string
  • html = """Advice
  • The IETF admonishes:
  • Be strict in what you send.

  •   

  • """

  • tagstack = []
  • class ShowStructure(HTMLParser.HTMLParser):
  •     def handle_starttag(self, tag, attrs): tagstack.append(tag)
  •     def handle_endtag(self, tag): tagstack.pop()
  •     def handle_data(self, data):
  •         if data.strip():
  •             for tag in tagstack: sys.stdout.write('/'+tag)
  •             sys.stdout.write(' >> %s/n' % data[:40].strip())
  • ShowStructure().feed(html)
  

此函数的输出:

/html/body/p >> The
/html/body/p/a >> IETF admonishes:
/html/body/p/a/i >> Be strict in what you
/html/body/p/a/i/b >> send
/html/body/p/a/i >> .


对于一些网页,可能并没有严格的开始结束标签对,这时,我们可以去忽略一些标签。可以自己写个堆栈来处理这些标签。





[python] view plaincopy

  • #*--------------- TagStack class example -----------------#
  • class TagStack:
  •     def __init__(self, lst=[]): self.lst = lst
  •     def __getitem__(self, pos): return self.lst[pos]
  •     def append(self, tag):
  •     # Remove every paragraph-level tag if this is one
  •     if tag.lower() in ('p','blockquote'):
  •         self.lst = [t for t in self.lst
  •                 if t not in ('p','blockquote')]
  •         self.lst.append(tag)
  •     def pop(self, tag):
  •         # "Pop" by tag from nearest pos, not only last item
  •         self.lst.reverse()
  •         try:
  •         pos = self.lst.index(tag)
  •         except ValueError:
  •             raise HTMLParser.HTMLParseError, "Tag not on stack"
  •         del self.lst[pos]
  •         self.lst.reverse()
  • tagstack = TagStack()
  



HTMLParser有个bug,就是不能处理中文属性,比如说,如果网页里有这么一段:


那么解析到这一行时就会出错。

  

错误原因还是正则表达式惹的祸。

attrfind = re.compile(
    r'/s*([a-zA-Z_][-.:a-zA-Z_0-9]*)(/s*=/s*'
    r'(/'[^/']*/'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$/(/)_#=~@]*))?')
attrfind 没有匹配中文字符。

可以更改这个匹配已修正这个错误。sgmllib则不存在这种错误。


2、sgmllib

html格式为sgml格式的一个子集,所以sgml可以处理跟多的东西,下面通过一段代码来示例sgmllib的用法。

  




[python] view plaincopy

  • #------------------ HTMLParser_stack.py ------------------#
  • #-- coding: GBK --
  • import sgmllib,sys,os,string
  • html = """Advice
  • The IETF admonishes:
  • Be strict in what you send.



  • """

  • os.chdir('d://python')
  • f=file('testboard.txt','r')
  • contest=f.read()
  • tagstack = []
  • class ShowStructure(sgmllib.SGMLParser):
  •     def handle_starttag(self, tag, method,attrs): tagstack.append(tag)
  •     def handle_endtag(self, tag): tagstack.pop()
  •     def handle_data(self, data):
  •         if data.strip():
  •             for tag in tagstack: sys.stdout.write('/'+tag)
  •             sys.stdout.write(' >> %s/n' % data[:40].strip())

  •     def unknown_starttag(self,tag,attrs):
  •         print 'start tag:'
  •     def unknown_endtag(self,tag):
  •         print 'end tag:'
  •     def start_lala(self,attr):
  •         print 'lala tag found'
  • ShowStructure().feed(html)
  
输出:


start tag:
start tag:
/lala >> Advice
end tag:
end tag:
start tag:
start tag:
/lala >> The
start tag:
/lala >> IETF admonishes:
start tag:
/lala >> Be strict in what you
start tag:
/lala >> send
end tag:
/lala >> .
end tag:
end tag:
end tag:
start tag:
start tag:
/lala >> ϒ
start tag:
end tag:
end tag:
end tag:


和HTMLParser一样,如果要用sgmllib解析html,则要继承sgmllib.SGMLParser类,此类里的函数都是空的,用户需要重载它。这个类提供的功能是在特定情况下调用相应的函数。

比如当发现标签时,如果并没有定义 start_html(self,attr)函数,则会调用unknown_starttag函数,具体怎么处理则更具用户。

sgml的标签是可以自定义的,比如自己定义了一个start_lala函数,则就会处理标签。


有个地方要说明下,如果定义了start_tagname函数,有定义了handle_starttag函数,则函数只会运行handle_starttag函数,start_tagname为空函数都没有问题,如果没有定义handle_starttag函数,则遇到标签时,会运行start_tagname函数。如果没有定义tagname的start函数,则此标签为未知标签,调用unknown_starttag函数

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-60070-1-1.html 上篇帖子: python的egg的制作 下篇帖子: Python 编码风格
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表