使用正则表达式或lxml在Python中提取HTML注释?

使用正则表达式或lxml在Python中提取HTML注释?,python,regex,Python,Regex,如何使用Python从文档中提取所有HTML样式的注释 我试过使用正则表达式: text = 'hello, world <!-- comment -->' re.match('<!--(.*?)-->', text) text='hello,world' 重新匹配(“”,文本) 但它什么也不产生。我不明白这一点,因为同一个正则表达式在同一个字符串上可以正常工作 更新:我的文档实际上是一个XML文件,我正在用pyquery(基于lxml)解析文档,但我认为lxml不能

如何使用Python从文档中提取所有HTML样式的注释

我试过使用正则表达式:

text = 'hello, world <!-- comment -->'
re.match('<!--(.*?)-->', text)
text='hello,world'
重新匹配(“”,文本)
但它什么也不产生。我不明白这一点,因为同一个正则表达式在同一个字符串上可以正常工作

更新:我的文档实际上是一个XML文件,我正在用pyquery(基于lxml)解析文档,但我认为lxml不能。这是文档的外观:

<?xml version="1.0" encoding="UTF-8"?>
<clinical_study rank="220398">
  <intervention_browse>
    <!-- CAUTION:  The following MeSH terms are assigned with an imperfect algorithm  -->
    <mesh_term>Freund's Adjuvant</mesh_term>
    <mesh_term>Keyhole-limpet hemocyanin</mesh_term>
  </intervention_browse>
  <!-- Results have not yet been posted for this study                                -->
</clinical_study>

弗氏佐剂
锁孔帽贝血蓝蛋白

更新2:感谢您提出另一个答案,但我已经在用lxml对文档进行广泛的解析,不想用BeautifulSoup重写所有内容。相应地更新了标题

您可以使用Beautiful Soup在这样的for循环中提取注释

from bs4 import BeautifulSoup, Comment

text = 'hello, world <!-- comment -->'

soup = BeautifulSoup(text, 'lxml')

for x in soup.findAll(text=lambda text:isinstance(text, Comment)):
    print(x)
来自bs4导入美化组的
,注释
text='你好,世界'
soup=BeautifulSoup(文本“lxml”)
对于soup.findAll中的x(text=lambda text:isinstance(text,Comment)):
打印(x)

您可以使用Beautiful Soup在这样的for循环中提取注释

from bs4 import BeautifulSoup, Comment

text = 'hello, world <!-- comment -->'

soup = BeautifulSoup(text, 'lxml')

for x in soup.findAll(text=lambda text:isinstance(text, Comment)):
    print(x)
来自bs4导入美化组的
,注释
text='你好,世界'
soup=BeautifulSoup(文本“lxml”)
对于soup.findAll中的x(text=lambda text:isinstance(text,Comment)):
打印(x)

匹配
更改为
搜索
然后:

text = 'hello, world <!-- comment -->'
comment = re.search('<!--(.*?)-->', text)
comment.group(1)

match
更改为
search
an,然后:

text = 'hello, world <!-- comment -->'
comment = re.search('<!--(.*?)-->', text)
comment.group(1)

必须使用re.findall()方法提取与特定模式匹配的所有子字符串


re.match()只会检查模式是否适合字符串开头,而re.search()只会获取字符串中的第一个匹配项。出于您的目的,re.findall()绝对是正确的方法,应该是首选。

您必须使用re.findall()方法来提取与特定模式匹配的所有子字符串


re.match()只会检查模式是否适合字符串开头,而re.search()只会获取字符串中的第一个匹配项。出于您的目的,re.findall()绝对是正确的方法,应该是首选方法。

这似乎是我的评论:

from lxml import etree
txt = """<?xml version="1.0" encoding="UTF-8"?>
<clinical_study rank="220398">
  <intervention_browse>
    <!-- CAUTION:  The following MeSH terms are assigned with an imperfect algorithm  -->
    <mesh_term>Freund's Adjuvant</mesh_term>
    <mesh_term>Keyhole-limpet hemocyanin</mesh_term>
  </intervention_browse>
  <!-- Results have not yet been posted for this study                                -->
</clinical_study>"""
root = etree.XML(txt)
print root[0][0]

这似乎是我的评论:

from lxml import etree
txt = """<?xml version="1.0" encoding="UTF-8"?>
<clinical_study rank="220398">
  <intervention_browse>
    <!-- CAUTION:  The following MeSH terms are assigned with an imperfect algorithm  -->
    <mesh_term>Freund's Adjuvant</mesh_term>
    <mesh_term>Keyhole-limpet hemocyanin</mesh_term>
  </intervention_browse>
  <!-- Results have not yet been posted for this study                                -->
</clinical_study>"""
root = etree.XML(txt)
print root[0][0]

XPath在这里工作得很好:
tree.XPath('//comment()')
。例如,从DOM中删除所有脚本、样式和注释可以执行以下操作:

tree = lxml.html.fromstring(html)
for el in tree.xpath('//script | //style | //comment()'):
    el.getparent.remove(el)

没有美化组。

XPath在这里工作得很好:
tree.XPath('//comment()')
。例如,从DOM中删除所有脚本、样式和注释可以执行以下操作:

tree = lxml.html.fromstring(html)
for el in tree.xpath('//script | //style | //comment()'):
    el.getparent.remove(el)

没有BeautifulSoup。

使用lxml或beautifulsoup@MaxU我已经在使用lxml(pyquery),所以我不想切换到BeautifulSoup,但谢谢。我已经更新了这个问题,以明确我很乐意使用regex或lxml。@Padraic我不确定它在lxml中是否可行,请参阅更新。@Richard dox您链接到的建议您可以确定
标记是否是
etree。注释
--您尝试过吗?然后,如果
True
可以只打印
标记
属性值?@DavidZemens的问题是没有
标记
,注释只是浮动的。使用lxml或beautifulsoup@MaxU我已经在使用lxml(pyquery),所以我不想切换到BeautifulSoup,但谢谢。我已经更新了这个问题,以明确我很乐意使用regex或lxml。@Padraic我不确定它在lxml中是否可行,请参阅更新。@Richard dox您链接到的建议您可以确定
标记是否是
etree。注释
--您尝试过吗?然后,如果
True
可以只打印
标记
属性值?@DavidZemens的问题是没有
标记
,注释只是浮动的。谢谢!这实际上是我关心的最后一条评论(在一个有任意数量评论的文档中,尽管最后一条评论总是在结束
clinical\u study
tag)之前),你知道怎么得到它吗?啊
root[0]
似乎就是这样。谢谢<代码>根(1)< /代码>为我打印<代码> <代码>。干杯,如果它工作的话,李察请考虑把这个答案标记为“接受”。谢谢!这实际上是我关心的最后一条评论(在一个有任意数量评论的文档中,尽管最后一条评论总是在结束
clinical\u study
tag)之前),你知道怎么得到它吗?啊
root[0]
似乎就是这样。谢谢<代码>根(1)< /代码>为我打印<代码> <代码>。干杯,如果它工作的话,李察应该考虑把这个答案标记为“接受”。