python 读取配置文件方式 获取股票

#! /usr/bin/python2
# coding=utf-8
import os
import threading
import urllib2
import time
from Tkinter import *
#from multiprocessing import Process
from multiprocessing import Process, Value, Array, Manager

val_price = 20
val_percent = 5 / 100

def func():
    os._exit(0)



def creatfram(name):
    root = Tk()
    w = Label(root, text=name)
    w.pack()
    root.geometry('150x20+0+0')
    root.resizable(width=False, height=False)
    root.mainloop()

#print sys.getdefaultencoding()
reload(sys)
sys.setdefaultencoding('utf-8')
#print sys.getdefaultencoding()

def get_price(code):
    url = 'http://hq.sinajs.cn/?list=%s' % code
    #print url
    req = urllib2.Request(url)
    #print req
    # 如果不需要设置代理,下面的set_proxy就不用调用了。由于公司网络要代理才能连接外网,所以这里有set_proxy…
    # req.set_proxy('proxy.XXX.com:911', 'http')
    content = urllib2.urlopen(req).read()
    str = content.decode('gbk', 'ignore')
    data = str.split('"')[1].split(',')
    name = "%-6s" % data[0]
    price_current = "%-6s" % float(data[3])
    change_percent = (float(data[3]) - float(data[2])) * 100 / float(data[2])
    change_percent = "%-6s" % round(change_percent, 2)
    #print("股票名称:{0} 涨跌幅:{1} 最新价:{2}".format(name, change_percent, price_current))

    return change_percent, price_current, name




def p_stock(code_list):
    """
    A doubling function that can be used by a process
    """

    for code in code_list:
        #print "-------------------",code['code']
        percent, price, alias = get_price(code['code'])
        #print code, price
        name = code['code'] + " " + percent + " " + price
        flg_show = 0
        #print code['code'], float(price), float(percent), " alarm ",code['price'], code['percent']
        if (((float(price) > code['price'] and 0 != code['price']) ) and (1 == code['flg_price'])):
            flg_show = 1
            code['flg_price'] = 0
        if (((float(percent) > code['percent'] and 0 != code['percent'])) and (1 == code['flg_percent'])):
            flg_show = 1
            code['flg_percent'] = 0
        if 1 == flg_show:
            print code['code'], alias, float(price), float(percent), " alarm ", code['price'], code['percent']
            timer = threading.Timer(5, func)
            timer.start()
            creatfram(name)

def stock_header(str):
    t1 = ('60', '900')
    t2 = ('000', '002', '300', '200')
    if str.startswith(t1):
        str2 = 'sh' + str
    elif str.startswith(t2):
        str2 = 'sz' + str
    else:
        str2 = str
        print str

    return str2

def file_read(A, mgr):
    file = open("date.txt",'rU')
    text = file.read()
    lines = text.split('\n')
    j=0
    for line in lines:
        # print line
        # 将每一行分隔开,提取姓名和联系方式
        i =0
        l = ['600127', '0', '0']
        for cell in line.split(','):
            if not cell:
                break
            l[i] = cell
            i += 1
        str = l[0]
        str2 = stock_header(str)
        #print i, "----------", str2, "----------", l[1], "----------", l[2]
        A.append(mgr.dict(code=str2, price=float(l[1]), percent=float(l[2]), flg_price=1, flg_percent=1))
        #print A[j]['code'], A[j]['price']
        j = j +1

        file.close()

    #return code,l[1],l[2]

if __name__ == '__main__':
    mgr = Manager()
    A = Manager().list()
    file_read(A, mgr)
    #for s in A:
    #    print s
    '''
    var1 = 'sz300127'
    var2 = 20
    var3 = 5
    A.append(mgr.dict(code=var1, price=var2, percent=var3, flg_price=1, flg_percent=1))
    A.append(mgr.dict(code='sz000639', price=20, percent=5, flg_price=1, flg_percent=1))
    A.append(mgr.dict(code='sh600718', price=0, percent=5, flg_price=1, flg_percent=1))
    A.append(mgr.dict(code='sh600452', price=0, percent=5, flg_price=1, flg_percent=1))
    A.append(mgr.dict(code='sh600489', price=0, percent=0, flg_price=1, flg_percent=1))
    #print A[0]['flg_price'], A[0]['flg_percent']
    '''
    time_interval = 60 * 10
    flg_time = 0
    old = 0
    while True:
        proc = Process(target=p_stock, args=(A, ))
        proc.start()
        proc.join()
        if 0 == flg_time:
            for code in A:
                if (0 == code['flg_price'] or 0 == code['flg_percent'] ):
                    flg_time = 1
                    old = time.time()
                    print "---------------- ------------", old
        if ((1 == flg_time) and ((time.time() - old) > (time_interval))) :
            flg_time = 0
            print "++++++++++++++++++++++", time.time()
            for code in A:
                code['flg_price'] = 1
                code['flg_percent'] = 1

#print A[0]['flg_price'], A[0]['flg_percent']

--------------------------------------------------date.txt----------

300127,20,5
000639,20,5
600718
600452
600489


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值