Python obtains the current data based on the stock code

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.