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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值