1
是否有一種簡單的方法來查找以<!--
開頭的HTML註釋的第一個實例?如何查找以<! - ?開頭的HTML註釋的第一個實例(Python)
我已經做了這樣:
for line in open('some.html'):
if line[:4] == "<!--":
print line
break
是否有一種簡單的方法來查找以<!--
開頭的HTML註釋的第一個實例?如何查找以<! - ?開頭的HTML註釋的第一個實例(Python)
我已經做了這樣:
for line in open('some.html'):
if line[:4] == "<!--":
print line
break
itertools.ifilter(lambda x:x.startswith('<!--'), open('test')).next()
使用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