如何在python中创建股票报价获取应用 [英] How to create a stock quote fetching app in python
问题描述
我对使用 Python 进行编程非常陌生.
I'm quite new to programming in Python.
我想制作一个可以从 Google财务 获取股票价格的应用程序>.一个示例是CSCO (思科系统).然后,我将使用这些数据当库存达到一定值时警告用户.它还需要每30秒刷新一次.
I want to make an application which will fetch stock prices from google finance. One example is CSCO (Cisco Sytems). I would then use that data to warn the user when the stock reaches a certain value. It also needs to refresh every 30 seconds.
问题是我不知道如何获取数据!
The problem is I dont have a clue how to fetch the data!
有人有什么主意吗?
推荐答案
此模块由科里·戈德堡.
程序:
import urllib
import re
def get_quote(symbol):
base_url = 'http://finance.google.com/finance?q='
content = urllib.urlopen(base_url + symbol).read()
m = re.search('id="ref_694653_l".*?>(.*?)<', content)
if m:
quote = m.group(1)
else:
quote = 'no quote available for: ' + symbol
return quote
样品用量:
import stockquote
print stockquote.get_quote('goog')
更新:更改了正则表达式以匹配Google财经的最新格式(截至2011年2月23日).这展示了依靠屏幕抓取时的主要问题.
Update: Changed the regular expression to match Google Finance's latest format (as of 23-Feb-2011). This demonstrates the main issue when relying upon screen scraping.
这篇关于如何在python中创建股票报价获取应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!