最新消息

[公告2014/05/30] 如有需要將部落格中,任何一篇文章的程式碼使用在商業用途,請與我聯繫。

[公告2015/04/26] Line版的 iInfo程式與投資應用 群組已上線想加入的朋友們,請先查看 "入群須知" 再與我聯繫 Line : aminwhite5168,加入請告知身分與回答 "入群須知" 的問題。

[公告2018/04/22] 台北 Python + Excel VBA 金融資訊爬蟲課程,課程如網頁內容 金融資訊爬蟲班:台北班 Python 金融資訊爬蟲、EXCEL VBA 金融資訊爬蟲

[公告2019/01/08] 請注意:我再次重申,部落格文章的程式碼,是要提供各位參考與學習,一旦網頁改版請自行修改,別要求東要求西要我主動修改,你們用我寫東西賺錢了、交差了,請問有分我一杯羹嗎?既然賺錢沒分我,請問有什麼理由要求我修改,如果沒能力改,就花錢來找我上課。

[公告2019/12/01] 若各位有 Excel VBA 案子開發需求,歡迎與我聯繫,可接案處理。

[公告2020/05/22] 頁面載入速度慢,起因為部分JS來源(alexgorbatchev.com)失效導致頁面載入變慢,目前已做調整,請多見諒。

2013年11月1日 星期五

補上缺少的Python函數

Python抓劵商公司,忘了附上lib在使用的函數,很抱歉,在此補上缺少的部分。
#劵商總公司
def Broker():
    while True:
        url = "http://www.twse.com.tw/ch/products/broker_service/broker_list.php"
        webcode = urllib.urlopen(url)
        if webcode.code == 200:
            stock = Broker_list()
            stock.parse(webcode.read())
            webcode.close()
            stock.close()

        if len(stock.brokerid) > 0 :
            return stock
        else:
            return 0

class Broker_list(SGMLParser):
    def __init__(self):
        SGMLParser.__init__(self)

    def reset(self):
        SGMLParser.reset(self)
        self.broker_id_open = False
        self.brokerid = []
        self.brokeropen = []        
        self.gray12count = 0

        self.broker_name = False
        self.brokername = []
        self.brokernamecount = 0
        
        self.gray12count2 = 0
        self.broker_address_tel = False
        self.brokeraddress = []
        self.brokertel = []
        self.tilcount = 0
        
    def parse(self,data):
        self.feed(data)
        self.close()
        
    def start_a(self,attrs):
        for name, value in attrs:            
            if name == 'class' and value == 'basictxt':
                self.broker_name = True
                self.brokernamecount += 1
                
    def start_td(self, attrs):        
        for name, value in attrs:
            #print attrs            
            if name == 'class':
                if value == 'til_2':
                    self.tilcount += 1 
                    
                if self.tilcount == 6 and value == 'gray12':
                    if len(attrs) == 1:
                        self.broker_id_open = True
                        self.gray12count += 1 

                    if len(attrs) == 2:
                        self.broker_address_tel = True
                        self.gray12count2 += 1            

    def handle_data(self, text):
        if self.broker_id_open:
            if self.gray12count == 1:
                self.brokerid.append(text)
                self.broker_id_open = False
                #print "broker_id : " + text
                    
            elif self.gray12count == 2:
                self.brokeropen.append(text)
                self.broker_id_open = False
                self.gray12count = 0
                #print "broker_open : " + text

        if self.broker_name:
            if self.brokernamecount == 1:
                #print "1 : " + text
                self.brokername.append(text)
                self.broker_name = False
                    
            elif self.brokernamecount == 2:
                #print "2 : " + text
                self.broker_name = False
                self.brokernamecount = 0


        if self.broker_address_tel:
            if self.gray12count2 == 1:
                #print "address : " + text
                self.broker_address_tel = False
                self.brokeraddress.append(text)
                    
            elif self.gray12count2 == 2:
                #print "tel : " + text
                self.broker_address_tel = False
                self.brokertel.append(text)
                self.gray12count2 = 0
#劵商分公司
def Broker2():
    while True:
        url = "http://www.twse.com.tw/ch/products/broker_service/broker2_list.php"
        brokerkind = "slave"
        webcode = urllib.urlopen(url)
        if webcode.code == 200:
            stock = Broker2_list()
            stock.parse(webcode.read())
            webcode.close()
            stock.close()
            
        if len(stock.brokerid) > 0 :
            return stock
        else:
            return 0

class Broker2_list(SGMLParser):
    def __init__(self):
        SGMLParser.__init__(self)

    def reset(self):
        SGMLParser.reset(self)
        self.bPrintDetail = False
        self.broker_id_open = False
        self.brokerid = []
        self.brokeropen = []        
        self.gray12count = 0

        self.broker_name = False
        self.brokername = []
        self.brokernamecount = 0
        
        self.gray12count2 = 0
        self.broker_address_tel = False
        self.brokeraddress = []
        self.brokertel = []
        self.tilcount = 0
        
    def parse(self,data):
        self.feed(data)
        self.close()
        
    def start_a(self,attrs):
        for name, value in attrs:            
            if name == 'class' and value == 'basictxt':
                self.broker_name = True
                self.brokernamecount += 1
                
    def start_td(self, attrs):        
        for name, value in attrs:
            #print attrs            
            if name == 'class':
                if value == 'til_2':
                    self.tilcount += 1 
                    
                if self.tilcount == 5 and value == 'gray12':
                    if len(attrs) == 1:
                        self.broker_id_open = True
                        self.gray12count += 1 

                    if len(attrs) == 2:
                        self.broker_address_tel = True
                        self.gray12count2 += 1            

    def handle_data(self, text):
        if self.broker_id_open:
            if self.gray12count == 1:
                self.brokerid.append(text)
                self.broker_id_open = False
                #print "broker_id : " + text
                    
            elif self.gray12count == 2:
                self.brokeropen.append(text)
                self.broker_id_open = False
                self.gray12count = 0
                #print "broker_open : " + text

        if self.broker_name:
            if self.brokernamecount == 1:
                data = text.replace(' ', '')
                #print "1 : " + data
                self.brokername.append(data)
                self.broker_name = False
                self.brokernamecount = 0

        if self.broker_address_tel:
            if self.gray12count2 == 1:
                #print "address : " + text
                self.broker_address_tel = False
                self.brokeraddress.append(text)
                    
            elif self.gray12count2 == 2:
                #print "tel : " + text
                self.broker_address_tel = False
                self.brokertel.append(text)
                self.gray12count2 = 0