【发布时间】:2017-12-26 21:25:05
【问题描述】:
我有一个 HTML 文件,其中包含;
<html>
<head></head>
<body><p>thanks god its Friday</p></body>
</html>a& 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 文件中 </html> 之后的所有内容,但 HTML 标记之后的所有字符串都不属于标记,其中一些只是嘈杂,所以我无法使用 Beautifulsoup 执行此操作,我不这样做'不知道对 HTML 文件使用正则表达式是否明智。
如何删除</html> 之后的字符串并写入 HTML 文件?
【问题讨论】:
-
如果文件可以作为单个字符串完整读取(包括换行符等),您可以搜索
</html>的位置,然后使用字符串索引删除之后的所有内容。 -
非常感谢也促使我学习自己。
标签: regex python-2.7 beautifulsoup