上班时间通过浏览器打开股票网站怕会被别人看到,没关系,试试在命令行下执行代码看数据就行了。输入sh,就可以查看到上证指数输入sz,就可以查看深圳指数输入cyb,就可以查看创业板指数其他的股票代码可以自己自定义,加入到字典中就行了
python版本2.7.3
#!/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