lxml.html删除节点树和tag对

# encoding: utf-8
import StringIO

from apihelper import info, info_save
from lxml import etree, html
from lxml.html.clean import Cleaner


strhtml = '''\
  <html>
   <head>
     <script type="text/javascript" src="evil-site"></script>
     <link rel="alternate" type="text/rss" src="evil-rss">
     <style>
       body {background-image: url(javascript:do_evil)};
       div {color: expression(evil)};
     </style>
   </head>
   <body οnlοad="evil_function()">
     <!-- I am interpreted for EVIL! -->
     <a href="javascript:evil_function()">a link</a>
     <a href="#" οnclick="evil_function()">another link</a>
     <p οnclick="evil_function()">a paragraph</p>
     <div style="display: none">secret EVIL!</div>
     <object> of EVIL! </object>
     <iframe src="evil-site"></iframe>
     <form action="evil-site">
       Password: <input type="password" name="password">
     </form>
     <blink>annoying EVIL!</blink>
     <a href="evil-site">spam spam SPAM!</a>
     <image src="evil!">
     <div id='nav' class='nav'>this is nav</div>
   </body>
  </html>'''

# strhtml = '''<html><head></head>
# <body><div>aaa</div></body>
# </html>'''

etree.DEBUG  = 1
print etree.LIBXML_VERSION
utf8_parser=html.HTMLParser(encoding='utf8')
# f = open(xmlFile)
# xml = f.read()
# f.close()
doc=html.fromstring(strhtml, parser=utf8_parser)
# weather = doc.xpath('body/object')
# doc.find('.//body/object').drop_tag()
# doc.find('.//body/object').drop_tree()
doc.find('.//body').drop_tree()
# print '\r\n'.join(doc)
# print info_save(doc)
# print html.tostring(doc)

转载于:https://www.cnblogs.com/zhang-pengcheng/p/4291496.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值