【问题标题】:Extract both CSS and HTML comments from HTML doc (Python)从 HTML doc (Python) 中提取 CSS 和 HTML 注释
【发布时间】:2015-08-24 22:55:36
【问题描述】:

我正在使用 Python 将 HMTL 代码传递到 BeautifulSoup,而我的输出因 HTML 注释而出错。我有这个 python 脚本来删除 HTML cmets,但它无法删除嵌套在 CSS cmets 中的 HTML cmets。

我的代码:

from bs4 import BeautifulSoup, Comment   

   input_text = ""

   for line in open('output.txt'):
           input_text+=line

  soup = BeautifulSoup(input_text)
  comments = soup.findAll(text=lambda text:isinstance(text, Comment))
  [comment.extract() for comment in comments]
  print soup

例如,它会从我的测试输入中删除所有 HTML cmets,除了:

<!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4;} @font-face {font-family:inherit;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman",serif;} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:purple; text-decoration:underline;} span.EmailStyle17 {mso-style-type:personal-reply; font-family:"Calibri",sans-serif; color:#1F497D;} .MsoChpDefault {mso-style-type:export-only; font-family:"Calibri",sans-serif;} @page WordSection1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in;} div.WordSection1 {page:WordSection1;} -->

这是输入中的一段代码,其中包括在其上运行我的脚本后成功删除的 2 个 cmets,以及未删除的注释:

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="Generator" content="Microsoft Word 15 (filtered medium)"> <!--[if !mso]><style>v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style><![endif]--><style><!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4;} @font-face {font-family:inherit;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman",serif;} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:purple; text-decoration:underline;} span.EmailStyle17 {mso-style-type:personal-reply; font-family:"Calibri",sans-serif; color:#1F497D;} .MsoChpDefault {mso-style-type:export-only; font-family:"Calibri",sans-serif;} @page WordSection1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in;} div.WordSection1 {page:WordSection1;} --></style><!--[if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="1026" /> </xml><![endif]--><!--[if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1" /> </o:shapelayout></xml><![endif]--> 

我不确定首先删除 CSS cmets 的最佳方法。我不需要费心删除 CSS cmets 的内容,只需删除 /* */,因为其余部分应该用它嵌套在其中的 HTML 注释剥离

【问题讨论】:

  • 无法重现问题。此评论位于输入 HTML 中的哪个位置?
  • 抱歉,我编辑了我的原始帖子以包含更多代码

标签: python html css beautifulsoup


【解决方案1】:

我解决了我的问题。我使用正则表达式删除了它们,对于任何好奇的人,这是我的新代码:

from bs4 import BeautifulSoup, Comment
import re

input_text = ""

for line in open('output.txt'):
    input_text+=line

#extract all CSS comments
text = re.sub('\/*', '', input_text)
text = re.sub('\*/', '', text)

soup = BeautifulSoup(text)

#extract all HTML comments
comments = soup.findAll(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]

print soup

【讨论】:

    猜你喜欢
    • 2012-05-20
    • 2021-09-09
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    相关资源
    最近更新 更多