我正在尝试从以下文本中获取482.75: <span id="yfs_l84_aapl">482.75</span>
我使用的正则表达式是:regex = '<span id="yfs_l84_[^.]*">(.+?)</span>'
并且有效。
但是我不明白的是为什么[^。] *在这里可以匹配aapl?我的理解是。表示除换行符外的任何字符;^表示取反。因此[^。]应该是换行符,[^。] *应该是任意数量的换行符。但是,该理论与现实世界的实现背道而驰。
任何帮助表示赞赏,并在此先感谢。
我使用的python代码:
import urllib
import re
htmlfile = urllib.urlopen("http://finance.yahoo.com/q?s=AAPL&ql=0")
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_[^.]*">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern, htmltext)
print "the price of of aapl is", price[0]
内[]
的.
手段只是一个点。而领导的^
意思是“除了...以外的任何东西”。
因此[^.]*
匹配零个或多个非点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句