Maison > développement back-end > Tutoriel Python > python根据股票代码获取当前数据

python根据股票代码获取当前数据

大家讲道理
Libérer: 2016-11-09 11:16:53
original
1819 Les gens l'ont consulté

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

python版本2.7.3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

#!/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 + &#39;--&#39;+codeDict[key]

  

    code = raw_input(&#39;please select a code: &#39;)

    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 &#39;总共花费时间&#39;+str(costTime)+&#39;秒&#39;

    count +=1

Copier après la connexion


Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal