【问题标题】:How to remove strings which does not belong to HTML tag in an HTML file如何删除 HTML 文件中不属于 HTML 标签的字符串
【发布时间】:2017-12-26 21:25:05
【问题描述】:

我有一个 HTML 文件,其中包含;

<html>
<head></head>
<body><p>thanks god its Friday</p></body>
</html>a&amp; ca-79069608498"
<div class="cont" id="aka"></div>
<footer>
<div class="tent"><div class="cont"></div>
<h2><img alt="dscdsc" height="18" src="dsc.png" srcset="" width="116"/></h2>


</div>
</footer>

 ipt> (window.NORLQ=window.NORLQ||[]).push(function(){var 
ns,i,p,img;ns=document.getElementsByTagName('noscript');for(i=0;i<ns.len)>-1){img=document.createEleight'));img.setAttribute('alt',p.getAttribute('data-alt'));p.parentNode.replaceChild(img,p);}}});/*]]>*/</script><script>(window.RLQ=window.RLQ||[]).push(function(

文件名是a.html

我想使用 Python 2.7 删除 HTML 文件中 &lt;/html&gt; 之后的所有内容,但 HTML 标记之后的所有字符串都不属于标记,其中一些只是嘈杂,所以我无法使用 Beautifulsoup 执行此操作,我不这样做'不知道对 HTML 文件使用正则表达式是否明智。

如何删除&lt;/html&gt; 之后的字符串并写入 HTML 文件?

【问题讨论】:

  • 如果文件可以作为单个字符串完整读取(包括换行符等),您可以搜索&lt;/html&gt; 的位置,然后使用字符串索引删除之后的所有内容。
  • 非常感谢也促使我学习自己。

标签: regex python-2.7 beautifulsoup


【解决方案1】:

Python 有一个名为 HTMLParser 的模块来处理这类问题。

虽然提议的 regexpr 目前似乎可以很好地处理您的问题,但当出现问题时调试可能会出现问题,因为它无法处理边缘情况 HTML

因此,我提出了一个HTMLParser 解决方案,它可以让您更好地控制其解析行为。

示例:

from HTMLParser import HTMLParser


class MyHTMLParser(HTMLParser):
    buffer = ""
    end_of_html = False

    def get_html(self):
        return self.buffer

    def handle_starttag(self, tag, attrs):
        if not self.end_of_html:
            value = "<" + tag
            for attr in attrs:
                value += attr[0] + "=" + attr[1]
            self.buffer += value + ">"

    def handle_data(self, data):
        if not self.end_of_html:
            self.buffer += data

    def handle_endtag(self, tag):
        if not self.end_of_html:
            self.buffer += "</" + tag + ">"
        if tag == "html":
            self.end_of_html = True


parser = MyHTMLParser();
parser.feed("""<html>
</div>
<head></head>
<body><p>thanks god its Friday</p></body>
</html>a&amp; ca-79069608498"
<div class="cont" id="aka"></div>
<footer>
<div class="tent"><div class="cont"></div>
<h2><img alt="dscdsc" height="18" src="dsc.png" srcset="" width="116"/></h2>


</div>
</footer>

 ipt> (window.NORLQ=window.NORLQ||[]).push(function(){var
ns,i,p,img;ns=document.getElementsByTagName('noscript');for(i=0;i<ns.len)>-1){img=document.createEleight'));img.setAttribute('alt',p.getAttribute('data-alt'));p.parentNode.replaceChild(img,p);}}});/*]]>*/</script><script>(window.RLQ=window.RLQ||[]).push(function(
        """)

print parser.get_html()

输出:

<html>
</div>
<head></head>
<body><p>thanks god its Friday</p></body>
</html>

【讨论】:

    【解决方案2】:
    a = open(path, "r").read()
    b = a.split('</html>', 1)[0]
    open(path, 'w').write(b)
    

    【讨论】:

      【解决方案3】:

      使用正则表达式

      import re
      ...
      newhtml = re.sub('</html>[\s\S.]+', '</html>', oldhtml)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-09
        • 1970-01-01
        • 2015-03-24
        • 2011-02-04
        • 2013-02-24
        • 1970-01-01
        相关资源
        最近更新 更多