如何使用lxml删除这样的注释而不会失去Apple iPhone 5s的价值(太空灰,16 GB)
<h1 class="_3eAQiD" data-reactid="144">
<!-- react-text: 145 -->
Apple iPhone 5s (Space Grey, 16 GB)
<!-- /react-text -->
</h1>
使用正则表达式。
import re
a = '''<h1 class="_3eAQiD" data-reactid="144">
<!-- react-text: 145 -->
Apple iPhone 5s (Space Grey, 16 GB)
<!-- /react-text -->
</h1>'''
print re.sub("(<!--.*?-->)", "", a, flags=re.MULTILINE)
结果:
<h1 class="_3eAQiD" data-reactid="144">
Apple iPhone 5s (Space Grey, 16 GB)
</h1>
使用lxml
import lxml.etree as et
x = et.fromstring(a, parser=et.HTMLParser(remove_comments=True))
print(et.tostring(x))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句