Python gets current data based on stock code

Source: Internet
Author: User
Work time through the browser to open the stock website afraid to be seen by others, it doesn't matter, try to execute code under the command line to see the data on the line.
Input sh, you can view the Shanghai Composite
Enter SZ, you can view the Shenzhen index
Enter CYB to view Gem index
Other stock codes can be customized and added to the dictionary.

Python version 2.7.3

#!/usr/bin/env python#-*-coding:utf-8-*-import urllib2import reimport datetime def getstockinfo (URL): "" "Get information based on URL" "" stocklist = [] request = Urllib2. Request (URL) response = Urllib2.urlopen (request) Stockstr = Response.read () stocklist = Stockstr.split (', ') r Eturn stocklist def printstock (List): "" "Printing 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 according to 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 #输入stock代码输出对应的价格信息 #code = raw_input (' Code: ') codedict = {' sh ': ' Shang Hai zq ', ' sz ': ' Shen Zheng zq ', ' cyb ': ' Chang ye ban ', ' 601788 ': ' Guang da Zheng Quan ', ' 000 651 ': ' ge li Dian qi ',} #http://hq.sinajs.cn/list=s_sh000001 (Shanghai market Query) #http://hq.sinajs.cn/list=s_sz399001 (Shenzhen market query) Count = 0;while (count<=100): #循环100次后再退出 # Loop dictionary for key in Codedict:print key + '--' +codedict[key] Co     de = 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 () c Osttime = (end_time-now_time). Seconds print ' total time spent ' +str (costtime) + ' s ' Count +=1
  • Related Article

    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.