最新消息

[公告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)失效導致頁面載入變慢,目前已做調整,請多見諒。

2012年8月11日 星期六

Python抓劵商公司

使用Python透過解析網頁元素來取得全台劵商總公司的資訊
#!/usr/bin/python
#coding=utf-8

#---------------------------------------------
#   抓劵商相關資料
#   Version : 1.1
#   Author : Amin white
#   Release Date : 2012-01-01
#   Python version : 2.7.2
#---------------------------------------------

import codecs, csv
from lib import Broker, Broker2


def CheckBrokerAttribute(bankid):
    Brokerlist = ["9300"] #華南永昌    
    for i in range(0, len(Brokerlist)):
        if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1]:
            return u'公銀'
        
    Brokerlist = ["1020", #合庫
                  "1030", #土銀
                  "1040", #台銀
                  "1110", #台企
                  "1230", #彰銀
                  "5380", #第一金
                  "7000"] #兆豐    
    for i in range(0, len(Brokerlist)):
        if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1] and bankid[2] == Brokerlist[i][2]:
            return u'公銀'
        
    Brokerlist = [ "1360", #港商麥格理
                   "1380", #港商里昂
                   "1400", #港商蘇皇
                   "1440", #美林
                   "1470", #台灣摩根士丹利
                   "1480", #美商高盛
                   "1520", #瑞士信貸
                   "1530", #港商德意志
                   "1560", #港商野村台北分公司
                   "1570", #港商法國興業
                   "1590", #花旗環球
                   "1650", #新加坡商瑞銀
                   "6640", #渣打商銀
                   "8440", #摩根大通
                   "8700", #花旗
                   "8890", #大和國泰
                   "8900", #法銀巴黎
                   "8910", #台灣巴克萊
                   "8960"] #香港上海匯豐
    for i in range(0, len(Brokerlist)):
        if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1] and bankid[2] == Brokerlist[i][2]:
            return u'外資'
        
    return ''

def CheckMembership(MasterBrokerid, bankid):
    Brokerlist = []
    Brokerlist = list(MasterBrokerid)
    removelist = ['9100', #群益
                  '9200', #凱基
                  '9300', #華南永昌
                  '9600', #富邦
                  '2200', #元大寶來
                  '9800'] #元大
    #刪除特殊分類
    for i in range(0, len(removelist)):
        Brokerlist.remove(removelist[i])

    #比對所隸屬的總公司
    for i in range(0, len(Brokerlist)):
        if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1] and bankid[2] == Brokerlist[i][2]:
            return Brokerlist[i]
        
    #比對特殊分類所隸屬的總公司
    Brokerlist = []
    Brokerlist = list(removelist)
    for i in range(0, len(Brokerlist)):
        if bankid[0] == Brokerlist[i][0] and bankid[1] == Brokerlist[i][1]:
            return Brokerlist[i]

    print "Unknow broker id : " + Brokerlist[i]
    return ''


def Brokernamelist():  
    Brokerlist = Broker()

    Brokerfile = 'D:\\stock_database\\Brokerfile.csv'
    writefile = file(Brokerfile, 'wb')
    writefile.write(codecs.BOM_UTF8)
    writer = csv.writer(writefile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
    writer.writerow([u'劵商代碼'.encode('utf8'), u'劵商名稱'.encode('utf8'), u'劵商他名'.encode('utf8'), \
                     u'劵商主從'.encode('utf8'), u'劵商屬性'.encode('utf8'), u'劵商隸屬'.encode('utf8'), \
                     u'劵商地址'.encode('utf8'), u'劵商電話'.encode('utf8'), u'劵商開業日期'.encode('utf8'),\
                     u'備註'.encode('utf8')])
    
    for i in range(0, len(Brokerlist.brokerid)):
        brokerid = Brokerlist.brokerid[i]
        if brokerid == '6010':
            brokername = unicode('犇亞', "utf-8")
        else:
            brokername = unicode(Brokerlist.brokername[i], "big5")
            
        brokername = brokername.encode('utf8')
        brokeraddress = unicode(Brokerlist.brokeraddress[i], "big5")
        brokertel = unicode(Brokerlist.brokertel[i], "utf-8") 
        brokeropen = unicode(Brokerlist.brokeropen[i], "utf-8") 
        brokerProperty = CheckBrokerAttribute(brokerid)
        Membership = CheckMembership(Brokerlist.brokerid, brokerid)

        writer.writerow([ '%s' %brokerid.encode('utf8'),  '%s' %brokername, '', \
                          '%s' %u'總公司'.encode('utf8'),  '%s' %brokerProperty.encode('utf8'), '%s' %Membership.encode('utf8'), \
                          '%s' %brokeraddress.encode('utf8'), '%s' %brokertel.encode('utf8'), '%s' %brokeropen.encode('utf8'), ''])

        print brokerid + " " + brokername + " " + brokerProperty.encode('utf8') + " " + "write to csv OK......"
        
    Brokerlist2 = Broker2()
    for i in range(0,len(Brokerlist2.brokerid)):
        brokerid = Brokerlist2.brokerid[i]
        if brokerid == '6012':
            brokername = unicode('犇亞-網路', "utf-8").encode('utf8')
        else:
            brokername = unicode(Brokerlist2.brokername[i], "big5").encode('utf8')
        
        brokeraddress = unicode(Brokerlist2.brokeraddress[i], "big5")
        brokertel = unicode(Brokerlist2.brokertel[i], "big5")
        brokeropen = unicode(Brokerlist2.brokeropen[i], "utf-8")
        brokerProperty = CheckBrokerAttribute(brokerid)
        Membership = CheckMembership(Brokerlist.brokerid, brokerid)
    
        writer.writerow([ '%s' %brokerid.encode('utf8'),  '%s' %brokername, '', \
                          '%s' %u'分公司'.encode('utf8'),  '%s' %brokerProperty.encode('utf8'), '%s' %Membership.encode('utf8'), \
                          '%s' %brokeraddress.encode('utf8'), '%s' %brokertel.encode('utf8'), '%s' %brokeropen.encode('utf8'), ''])
        
        print brokerid + " " + brokername + " " + Membership + " " + "write to database OK......"

    writefile.close()
        

def main():
    Brokernamelist()
    
            
if __name__ == "__main__":
    main()

Python執行畫面如下圖所示

CSV檔案儲存內容如下