Python 簡小股票價格讀取程序
来源:互联网 发布:淘宝宝贝主图模板 编辑:程序博客网 时间:2024/04/29 22:07
金融問題,好像和中國的股市不相干似的,天天在升。而香港的股市沒有跟隨美國股市帶領,轉向由中資股主導的市場。
然而,股市是很波動的,股市數據也很大量,作為一個普通人,如何穫取自已的所須信息呢?
<script type="text/javascript"><!-- google_ad_client = "pub-9858973754388865";/* 468x60, created 17/10/09 */google_ad_slot = "7218176094";google_ad_width = 468;google_ad_height = 60;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
以下是由python 制作的小小股票價格讀取器,示範用途,功能不完善
程式環境要求
- Python 2.5 or Python 2.6
- Python lxml modules
在此就不仔細說如何,安裝lxml modules 了,請查詢其它相關文章
在未進行編程時,先到我們的目標,例如Yahoo! Finance,用IE Developer Tool, 或Firefox 的firebug.快速的讀取源代碼,該價格小格的HTML DOM Tree 識別符號,這有利於我拆解目標網站的資料,如上圖所顯示,我們找到了 ID Tag,但價格那個元素里沒有ID Tag,怎麼做呢,我們先用ID tag 找到我們要的區域,因為ID Tag理應在整個HTML 里只有一個,所以,我們可以很準確找到要的元素,然後再用Class 來找就沒什麼問題了。
以下是整個程式代碼
- import threading
- import urllib
- import lxml.html
- import time
- class WorkerThread(threading.Thread):
- def __init__(self,task):
- self.task = task
- threading.Thread.__init__(self)
- def run(self):
- while True:
- print 'Runging ....'
- if self.task != None:
- url = 'http://hk.finance.yahoo.com/q?s=%s.hk'%task
- print 'url:%s'%url
- f = urllib.urlopen(url)
- htmlContent = f.read()
- f.close()
- print 'finish read web content'
- html = lxml.html.fromstring(htmlContent)
- box = html.get_element_by_id('quote-bar-latest')
- vals = box.find_class('price')
- if vals is not None and len(vals) > 1:
- print 'code: %s, Price: %s'%(self.task,vals[0])
- time.sleep(5.0)
- def StartWorker(taskList = []):
- for task in taskList:
- WorkerThread(task).start()
- tasks = []
- tasks.append('0005')
- tasks.append('2007')
- StartWorker(tasks)
這段代碼,只說明了,我們能通過一些大型網絡讀取股票數據,在某些網站,他們的伺服器是做了一些設置,我們并不能大量的讀取,不然,會對我的IP 進行封鎖。也許你會問,我要這數據做什麼用呢。在金融界里,有兩派,一種是依靠技術分析,說的就是用過往股價表現,用特定方程式,估數,股票到達某個價位,就為買入信號,當然,也有賣出信號。而這類分析,就很依賴股價數據。在往後幾篇,我會完善這個小程式,當然,依然會使用Python 作為工具
<script type="text/javascript"><!-- google_ad_client = "pub-9858973754388865";/* 468x60, created 17/10/09 */google_ad_slot = "7218176094";google_ad_width = 468;google_ad_height = 60;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
- Python 簡小股票價格讀取程序
- 股票的小程序
- 用python写的股票跑马灯小程序
- 一个股票分析小程序及源代码
- WinForm实现股票实时查询小程序
- 隐形的桌面股票小程序 STK 2.0.3.1
- python小程序
- python 小程序
- python小程序
- python 简单小程序
- python小程序集锦
- python爬虫小程序
- Python小程序
- python小程序
- python练习小程序
- Python入门小程序
- python 查询小程序
- [python] 小程序
- jbpm源代码研究(二)-------IOC容器解析器
- 深圳杂记 10-16-2009
- 使用C#对XML进行增删改查操作
- volatile
- ASP.Net生成静态页面
- Python 簡小股票價格讀取程序
- 免费的内存泄漏检测工具Visual Leak Detector
- 圆与矩形简单碰撞检测
- Javascript拓展方法
- 美国体验圣帕特里克节(St.Patrick's Day)
- Expression Web设计工具
- 给全体移动员工都发个OPhone手机吧
- 评点联通iPhone策略:明修栈道、暗渡陈仓
- 联通版iPhone依然风韵犹存