我试图从Google Finance使用python3刮取一些股票价格和变体,但我无法弄清楚如果页面或我的正则表达式出现问题。我在想,整个页面中的svg图形或许多脚本标记都会使正则表达式解析器无法正确分析代码。无法解析Google财经html
我在许多在线正则表达式构建器/测试器上测试了这个正则表达式,它看起来没问题。无论如何,正如专为HTML设计的正则表达式一样。
的谷歌财经页面我在测试这一点是https://www.google.com/finance?q=NYSE%3AAAPL 我的Python代码如下
import urllib.request
import re
page = urllib.request.urlopen('https://www.google.com/finance?q=NYSE%3AAAPL')
text = page.read().decode('utf-8')
m = re.search("id=\"price-panel.*>(\d*\d*\d\.\d\d)</span>.*\((-*\d\.\d\d%)\)", text, re.S)
print(m.groups())
这将提取的股票价格及其变动百分比。 我一直在使用python2 + BeautifulSoup也试过,像这样
soup.find(id='price-panel')
但即使是这样一个简单的查询返回空。这尤其是为什么我认为这有点奇怪的HTML。
而这里的是我的目标
<div id="price-panel" class="id-price-panel goog-inline-block">
<div>
<span class="pr">
<span class="unchanged" id="ref_22144_l"><span class="unchanged">96.41</span><span></span></span>
</span>
<div class="id-price-change nwp goog-inline-block">
<span class="ch bld"><span class="down" id="ref_22144_c">-1.13</span>
<span class="down" id="ref_22144_cp">(-1.16%)</span>
</span>
</div>
</div>
<div>
<span class="nwp">
Real-time:
<span class="unchanged" id="ref_22144_ltt">3:42PM EDT</span>
</span>
<div class="mdata-dis">
<span class="dis-large"><nobr>NASDAQ
real-time data -
<a href="//www.google.com/help/stock_disclaimer.html#realtime" class="dis-large">Disclaimer</a>
</nobr></span>
<div>Currency in USD</div>
</div>
</div>
</div>
我想知道如果你们任何人都遇到类似的问题,此页面和/或可以计算出,如果有什么事,HTML中最重要的位我的代码错了。提前致谢!
仅供参考,https://www.quandl.com/help/api-for-stock-data我不知道Google Finance需要什么,但您可以从此处获得。 – user2023861 2014-10-16 20:50:23
@ user2023861谢谢,我会检查出来的。我曾经搜索过其他来源的数据,但没有发现我拥有的所有股票。我试图从除纽约证券交易所以外的交易所获得股票。 – Slpk 2014-10-17 14:01:40