Python obtains the current data according to the stock code. the code is shared. when you open the stock website through a browser, you will be afraid of being seen by others. it doesn't matter. just execute the code under the command line to view the data.
Enter sh to view the Shanghai stock exchange index.
Enter sz to view the Shenzhen index
Enter cyb to view the Growth Enterprise Index
You can customize other stock codes and add them to the dictionary.
Python version 2.7.3
#! /Usr/bin/env python #-*-coding: UTF-8-*-import urllib2import reimport datetime def getStockInfo (url ): "Get information by url" stockList = [] request = urllib2.Request (url) response = urllib2.urlopen (request) stockStr = response. read () stockList = stockStr. split (',') return stockList def printStock (List ): "print related information" "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): "Get detailed url based on code" 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 # enter the stock code to output the corresponding price information # code = raw_input ('code:') codeDict = {'sh': 'Shang hai zq ', 'sz': 'Shen zheng zq', 'cyb': 'Chang ye ban ', '000000': 'guang da zheng quan', '20140901 ': 'GE li dian qi ',}# http://hq.sinajs.cn/list=s_ Sh000001 (Shanghai dashboard query )# http://hq.sinajs.cn/list=s_ Sz399001 (Shenzhen dashboard query) count = 0; while (count <= 100): # loop 100 times and then exit # loop dictionary for key in codeDict: print key + '--' + codeDict [key] code = raw_input ('Please select a code: ') now_time = datetime. datetime. now () # print the code url = getUrlByCode (code) stockInfo = getStockInfo (url) # print stockInfo printStock (stockInfo) end_time = datetime. datetime. now () costTime = (end_time-now_time ). seconds print 'total consumed Time' + str (costTime) + 'second' count + = 1