2013-05-05 24 views

回答

2
itertools.ifilter(lambda x:x.startswith('<!--'), open('test')).next() 
4

使用BeautifulSoup模块:

>>> from bs4 import BeautifulSoup, Comment 
>>> html = """ 
... Pineapples 
... <!-- I'm a sneaky comment --> Text 
... 
... Text <!-- Cabbages --> 
... """ 
>>> soup = BeautifulSoup(html) 
>>> print soup.find(text=lambda text:isinstance(text,Comment)).strip() 
I'm a sneaky comment 
相关问题