python获取股票数据_python根据股票代码获取当前数据
1.[代碼][Python]代碼
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import re
import datetime
def getStockInfo(url):
"""根據url獲取信息"""
stockList = []
request = urllib2.Request(url)
response = urllib2.urlopen(request)
stockStr = response.read()
stockList = stockStr.split(',')
return stockList
def printStock(List):
"""打印相關信息"""
print '***********price*****************' + List[1]
print '***********float_price***********' + List[2]
print '***********float_perct***********' + List[3] + '%'
print '***********succ_unit*************' + List[4]+' shou'
print '***********succ_price************' + List[5]
def getUrlByCode(code):
"""根據代碼獲取詳細的url"""
url = ''
stockCode = ''
if code == 'sh':
url = 'http://hq.sinajs.cn/list=s_sh000001'
elif code == 'sz':
url = 'http://hq.sinajs.cn/list=s_sz399001'
elif code == 'cyb':
url = 'http://hq.sinajs.cn/list=s_sz399006'
else:
pattern = re.compile(r'^60*')
match = pattern.match(code)
if match:
stockCode = 'sh'+ code
else:
stockCode = 'sz' + code
url = 'http://hq.sinajs.cn/list=s_'+stockCode
return url
#輸入stock代碼輸出對應的價格信息
#code = raw_input('code: ')
codeDict = {
'sh' : 'shang hai zq',
'sz' : 'shen zheng zq',
'cyb' : 'chang ye ban',
'601788' : 'guang da zheng quan',
'000651' : 'ge li dian qi',
}
#http://hq.sinajs.cn/list=s_sh000001 (上海大盤查詢)
#http://hq.sinajs.cn/list=s_sz399001 (深圳大盤查詢)
count = 0;
while (count<=100):#循環100次后再退出
# 循環字典
for key in codeDict:
print key + '--'+codeDict[key]
code = raw_input('please select a code: ')
now_time = datetime.datetime.now()
#打印該code的信息
url = getUrlByCode(code)
stockInfo = getStockInfo(url)
#print stockInfo
printStock(stockInfo)
end_time = datetime.datetime.now()
costTime = (end_time - now_time).seconds
print '總共花費時間'+str(costTime)+'秒'
count +=1
總結
以上是生活随笔為你收集整理的python获取股票数据_python根据股票代码获取当前数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无穷大正整数 python_python
- 下一篇: 树莓派python gpio 模仿iic