def get_data(self, daycode, DEBUG=False ): stock_list = [] #check status from access log to decide if return directly if (self.status_check_return_now(daycode) == True ): return stock_list #1.Get soup object if (DEBUG == False): self._config_url(daycode, 'ALLBUT0999') else: self._config_url(daycode, '01') soup = self._get_bs_obj(daycode) if(soup == None): return stock_list #2.Try to find "每日收盤行情" table table = None for tb in soup.find_all("table"): if( len( tb.find_all(string=re.compile("每日收盤行情"))) == 1 ): table =tb #table found break if (table == None): self.access_log_set(daycode, AL_STATUS.EMPTY) print("table not valid.") return stock_list #3. Fields Name fields_name_tr = table.select("thead tr")[1] data_tbody = table.select("tbody")[0] #4. Parse Table stock_list = self.parse_table(fields_name_tr, data_tbody) #5. Append market for stock in stock_list: stock["daycode"] = daycode.to_int() stock["market"] = "TWSE" #mapping if stock['漲跌(+/-)'] == '-': stock['change'] = 0 - Utils.to_float(stock['漲跌價差'].replace(",", "")) else: stock['change'] = Utils.to_float(stock['漲跌價差'].replace(",", "")) #6. Info self.access_log_set(daycode, AL_STATUS.OK, param1=str(len(stock_list)) ) print("上市行情 (%s): %d stocks done." % (daycode, len(stock_list))) #7. return return stock_list
def get_data(self, daycode, DEBUG=False ): stock_list = [] #check status from access log to decide if return directly if (self.status_check_return_now(daycode) == True ): return stock_list #1.Get soup object if (DEBUG == False): if ( daycode.to_int() >= 20120529 ): #Use EW after 101/05/29 self._config_url_print_format(daycode, 'EW') else: self._config_url_print_format(daycode, 'AL') else: self._config_url_print_format(daycode, '02') soup = self._get_bs_obj(daycode) if(soup == None): return stock_list #2.Try to find "每日收盤行情" table table = None for tb in soup.find_all("table"): if( len( tb.find_all(string=re.compile("每日收盤行情"))) == 1 ): table =tb #table found break if (table == None): self.access_log_set(daycode, AL_STATUS.EMPTY) print("table not valid.") return stock_list #3. Fields Name fields_name_tr = table.select("thead tr")[1] data_tbody = table.select("tbody")[0] #4. Parse Table stock_list = self.parse_table(fields_name_tr, data_tbody) #5. Append market for stock in stock_list: stock["daycode"] = daycode.to_int() stock["market"] = "TPEX" stock["change"] = Utils.to_float( stock["漲跌"].replace(" ","") ) #mapping stock['證券代號'] = stock['代號'] stock['證券名稱'] = stock['名稱'] stock['成交金額'] = stock['成交金額(元)'] stock['開盤價'] = stock['開盤'] stock['最高價'] = stock['最高'] stock['最低價'] = stock['最低'] stock['收盤價'] = stock['收盤'] stock['最後揭示買價'] = stock['最後買價'] stock['最後揭示買量'] = "" stock['最後揭示賣價'] = stock['最後賣價'] stock['最後揭示賣量'] = "" #6. Info self.access_log_set(daycode, AL_STATUS.OK, str(len(stock_list))) print("上櫃行情 (%s): %d stocks done." % (daycode, len(stock_list))) #7. return return stock_list
def _update_daily_quotation(self, row, stock ): try: row['daycode'] = stock['daycode'] row['change'] = stock['change'] row['volume'] = Utils.to_int(stock['成交股數']) row['deal'] = Utils.to_int(stock['成交筆數']) row['amount'] = Utils.to_int(stock['成交金額']) row['open_price'] = Utils.to_float(stock['開盤價']) row['top_price'] = Utils.to_float(stock['最高價']) row['low_price'] = Utils.to_float(stock['最低價']) row['close_price'] = Utils.to_float(stock['收盤價']) row['final_reveal_buy_price'] = Utils.to_float(stock['最後揭示買價']) row['final_reveal_buy_vol'] = Utils.to_int(stock['最後揭示買量']) row['final_reveal_sell_price'] = Utils.to_float(stock['最後揭示賣價']) row['final_reveal_sell_vol'] = Utils.to_int(stock['最後揭示賣量']) except: raise ValueError('Parsing Error!') #Update StockInfo if this is not found in StockInfo if ( self.stockinfo_get_by_code( stock["證券代號"]) == None ): new_stockinfo = StockInfo() new_stockinfo.market = stock['market'] new_stockinfo.code = stock['證券代號'] new_stockinfo.name = stock['證券名稱'] new_stockinfo.tags = ("UNKNOWN",) self.stockinfo_set(new_stockinfo)
def _update_daily_quotation(self, row, stock): try: row['daycode'] = stock['daycode'] row['change'] = stock['change'] row['volume'] = Utils.to_int(stock['成交股數']) row['deal'] = Utils.to_int(stock['成交筆數']) row['amount'] = Utils.to_int(stock['成交金額']) row['open_price'] = Utils.to_float(stock['開盤價']) row['top_price'] = Utils.to_float(stock['最高價']) row['low_price'] = Utils.to_float(stock['最低價']) row['close_price'] = Utils.to_float(stock['收盤價']) row['final_reveal_buy_price'] = Utils.to_float(stock['最後揭示買價']) row['final_reveal_buy_vol'] = Utils.to_int(stock['最後揭示買量']) row['final_reveal_sell_price'] = Utils.to_float(stock['最後揭示賣價']) row['final_reveal_sell_vol'] = Utils.to_int(stock['最後揭示賣量']) except: raise ValueError('Parsing Error!') #Update StockInfo if this is not found in StockInfo if (self.stockinfo_get_by_code(stock["證券代號"]) == None): new_stockinfo = StockInfo() new_stockinfo.market = stock['market'] new_stockinfo.code = stock['證券代號'] new_stockinfo.name = stock['證券名稱'] new_stockinfo.tags = ("UNKNOWN", ) self.stockinfo_set(new_stockinfo)