小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Python獲取股票歷史數(shù)據(jù)和收盤數(shù)據(jù)的代碼實現(xiàn)

 imelee 2016-12-17
#-*- coding: UTF-8 -*-
'''
Created on 201532
@author: Casey
'''
import time
import threading
'''
上證編碼:'600001' .. '602100'
深圳編碼:'000001' .. '001999'
中小板:'002001' .. '002999'
創(chuàng)業(yè)板:'300001' .. '300400'
'''
import urllib2
from datetime import date
from db import *
from setting import *
class StockTencent(object):
    #數(shù)據(jù)庫表
    __stockTables = {'cash':'stock_cash_tencent','quotation':'stock_quotation_tencent'}
    '''初始化'''
    def __init__(self):
       self.__logger = LoggerFactory.getLogger('StockTencent')
       self.__dbOperator = DBOperator()
         
    def main(self):
        self.__dbOperator.connDB()
        threading.Thread(target = self.getStockCash()).start()
        threading.Thread(target = self.getStockQuotation()).start()
        self.__dbOperator.closeDB()
          
    '''查找指定日期股票流量'''
    def __isStockExitsInDate(self, table, stock, date):
        sql = "select * from " + table + " where code = '%s' and date='%s'" % (stock, date)
        n = self.__dbOperator.execute(sql)
        if n >= 1:
            return True
        
    '''獲取股票資金流明細(xì)'''
    def __getStockCashDetail(self, dataUrl):
        #讀取數(shù)據(jù)
        tempData = self.__getDataFromUrl(dataUrl)
          
        if tempData == None:
            time.sleep(10)
            tempData = self.__getDataFromUrl(dataUrl)
            return False
                 
        #解析資金流向數(shù)據(jù)
        stockCash = {}
        stockInfo = tempData.split('~')
        if len(stockInfo) < 13: return
        if len(stockInfo) != 0 and stockInfo[0].find('pv_none') == -1:
            table = self.__stockTables['cash']
            code = stockInfo[0].split('=')[1][2:]
            date = stockInfo[13]
            if not self.__isStockExitsInDate(table, code, date):
                stockCash['code'] = stockInfo[0].split('=')[1][2:]
                stockCash['main_in_cash']     = stockInfo[1]
                stockCash['main_out_cash']    = stockInfo[2]
                stockCash['main_net_cash']    = stockInfo[3]
                stockCash['main_net_rate']    = stockInfo[4]
                stockCash['private_in_cash']  = stockInfo[5]
                stockCash['private_out_cash'] = stockInfo[6]
                stockCash['private_net_cash'] = stockInfo[7]
                stockCash['private_net_rate'] = stockInfo[8]
                stockCash['total_cash']       = stockInfo[9]
                stockCash['name']             = stockInfo[12].decode('utf8')
                stockCash['date']             = stockInfo[13]   
                #插入數(shù)據(jù)庫
                self.__dbOperator.insertIntoDB(table, stockCash)
   
    '''獲取股票交易信息明細(xì)'''
    def getStockQuotationDetail(self, dataUrl):
        tempData = self.__getDataFromUrl(dataUrl)
          
        if tempData == None:
            time.sleep(10)
            tempData = self.__getDataFromUrl(dataUrl)
            return False
             
        stockQuotation = {}
        stockInfo = tempData.split('~')
        if len(stockInfo) < 45: return
        if len(stockInfo) != 0 and stockInfo[0].find('pv_none') ==-1 and stockInfo[3].find('0.00') == -1:
            table = self.__stockTables['quotation']
            code = stockInfo[2]
            date = stockInfo[30]
            if not self.__isStockExitsInDate(table, code, date):
                stockQuotation['code']  = stockInfo[2]
                stockQuotation['name']  = stockInfo[1].decode('utf8')
                stockQuotation['price'] = stockInfo[3]
                stockQuotation['yesterday_close']   = stockInfo[4]
                stockQuotation['today_open']        = stockInfo[5]
                stockQuotation['volume']            = stockInfo[6]
                stockQuotation['outer_sell']        = stockInfo[7]
                stockQuotation['inner_buy']         = stockInfo[8]
                stockQuotation['buy_one']           = stockInfo[9]
                stockQuotation['buy_one_volume']    = stockInfo[10]
                stockQuotation['buy_two']           = stockInfo[11]
                stockQuotation['buy_two_volume']    = stockInfo[12]
                stockQuotation['buy_three']         = stockInfo[13]
                stockQuotation['buy_three_volume']  = stockInfo[14]
                stockQuotation['buy_four']          = stockInfo[15]
                stockQuotation['buy_four_volume']   = stockInfo[16]
                stockQuotation['buy_five']          = stockInfo[17]
                stockQuotation['buy_five_volume']   = stockInfo[18]
                stockQuotation['sell_one']          = stockInfo[19]
                stockQuotation['sell_one_volume']   = stockInfo[20]
                stockQuotation['sell_two']          = stockInfo[22]
                stockQuotation['sell_two_volume']   = stockInfo[22]
                stockQuotation['sell_three']        = stockInfo[23]
                stockQuotation['sell_three_volume'] = stockInfo[24]
                stockQuotation['sell_four']         = stockInfo[25]
                stockQuotation['sell_four_volume']  = stockInfo[26]
                stockQuotation['sell_five']         = stockInfo[27]
                stockQuotation['sell_five_volume']  = stockInfo[28]
                stockQuotation['datetime']          = stockInfo[30]
                stockQuotation['updown']            = stockInfo[31]
                stockQuotation['updown_rate']       = stockInfo[32]
                stockQuotation['heighest_price']    = stockInfo[33]
                stockQuotation['lowest_price']      = stockInfo[34]
                stockQuotation['volume_amout']      = stockInfo[35].split('/')[2]
                stockQuotation['turnover_rate']     = stockInfo[38]
                stockQuotation['pe_rate']           = stockInfo[39]
                stockQuotation['viberation_rate']   = stockInfo[42]
                stockQuotation['circulated_stock']  = stockInfo[43]
                stockQuotation['total_stock']       = stockInfo[44]
                stockQuotation['pb_rate']           = stockInfo[45]
                self.__dbOperator.insertIntoDB(table, stockQuotation)
    '''讀取信息'''
    def __getDataFromUrl(self, dataUrl):
        r = urllib2.Request(dataUrl)
        try:
            stdout = urllib2.urlopen(r, data=None, timeout=3)
        except Exception,e:
            self.__logger.error(">>>>>> Exception: " +str(e))  
            return None
          
        stdoutInfo = stdout.read().decode(params.codingtype).encode('utf-8')
        tempData = stdoutInfo.replace('"', '')
        self.__logger.debug(tempData)
        return tempData
       
    '''獲取股票現(xiàn)金流量'''  
    def getStockCash(self):
        self.__logger.debug("開始:收集股票現(xiàn)金流信息")
        try:
            #滬市股票
            for code in range(600001, 602100):
                dataUrl = "http://qt./q=ff_sh%d" % code
                self.__getStockCashDetail(dataUrl)
                  
            #深市股票
            for code in range(1, 1999):
                dataUrl = "http://qt./q=ff_sz%06d" % code
                self.__getStockCashDetail(dataUrl) 
                     
            #中小板股票
            for code in range(2001, 2999):
                dataUrl = "http://qt./q=ff_sz%06d" % code
                self.__getStockCashDetail(dataUrl)     
              
            #'300001' .. '300400'
            #創(chuàng)業(yè)板股票
            for code in range(300001, 300400):
                dataUrl = "http://qt./q=ff_sz%d" % code
                self.__getStockCashDetail(dataUrl)   
          
        except Exception as err:
            self.__logger.error(">>>>>> Exception: " +str(code) + " " + str(err))
        finally:
            None
        self.__logger.debug("結(jié)束:股票現(xiàn)金流收集")
          
    '''獲取股票交易行情數(shù)據(jù)'''
    def getStockQuotation(self):
        self.__logger.debug("開始:收集股票交易行情數(shù)據(jù)")
        try:
            #滬市股票
            for code in range(600001, 602100):
                dataUrl = "http://qt./q=sh%d" % code
                self.getStockQuotationDetail(dataUrl)  
         
            #深市股票
            for code in range(1, 1999):
                dataUrl = "http://qt./q=sz%06d" % code
                self.getStockQuotationDetail(dataUrl) 
           
            #中小板股票
            for code in range(2001, 2999):
                dataUrl = "http://qt./q=sz%06d" % code
                self.getStockQuotationDetail(dataUrl)    
              
            #'300001' .. '300400'
            #  創(chuàng)業(yè)板股票
            for code in range(300001, 300400):
                dataUrl = "http://qt./q=sz%d" % code
                self.getStockQuotationDetail(dataUrl)    
          
        except Exception as err:
            self.__logger.error(">>>>>> Exception: " +str(code) + " " + str(err))
        finally:
            None
        self.__logger.debug("結(jié)束:收集股票交易行情數(shù)據(jù)")
       
if __name__ == '__main__':
    StockTencent(). main()

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多