2012年9月6日 星期四

還是很髒很髒的 Python script


有趣的結果:

{'StockSymbol': '1414', 'K': '39.20', 'ROE': '24.63', 'ClosePrice': '9.03', 'BookValue': '14.37'}
{'StockSymbol': '1416', 'K': '16.84', 'ROE': '14.78', 'ClosePrice': '15.55', 'BookValue': '17.72'}
{'StockSymbol': '1460', 'K': '22.16', 'ROE': '18.60', 'ClosePrice': '8.20', 'BookValue': '9.77'}
{'StockSymbol': '1808', 'K': '22.82', 'ROE': '49.08', 'ClosePrice': '39.20', 'BookValue': '18.23'}
{'StockSymbol': '2498', 'K': '18.72', 'ROE': '52.13', 'ClosePrice': '254.00', 'BookValue': '91.21'}
{'StockSymbol': '2501', 'K': '19.71', 'ROE': '19.96', 'ClosePrice': '12.85', 'BookValue': '12.69'}
{'StockSymbol': '2611', 'K': '42.12', 'ROE': '41.27', 'ClosePrice': '15.60', 'BookValue': '15.92'}
{'StockSymbol': '2820', 'K': '33.23', 'ROE': '23.19', 'ClosePrice': '10.65', 'BookValue': '15.26'}
{'StockSymbol': '2888', 'K': '15.08', 'ROE': '14.66', 'ClosePrice': '8.08', 'BookValue': '8.31'}
{'StockSymbol': '3052', 'K': '15.38', 'ROE': '12.80', 'ClosePrice': '10.50', 'BookValue': '12.62'}
{'StockSymbol': '3056', 'K': '23.32', 'ROE': '38.19', 'ClosePrice': '25.50', 'BookValue': '15.57'}
{'StockSymbol': '4306', 'K': '16.36', 'ROE': '21.32', 'ClosePrice': '23.45', 'BookValue': '17.99'}
{'StockSymbol': '5508', 'K': '20.08', 'ROE': '46.17', 'ClosePrice': '55.70', 'BookValue': '24.23'}
{'StockSymbol': '5511', 'K': '21.08', 'ROE': '34.90', 'ClosePrice': '31.85', 'BookValue': '19.24'}
{'StockSymbol': '5515', 'K': '17.08', 'ROE': '16.78', 'ClosePrice': '15.00', 'BookValue': '15.27'}
{'StockSymbol': '6163', 'K': '16.69', 'ROE': '22.44', 'ClosePrice': '19.75', 'BookValue': '14.69'}
{'StockSymbol': '9906', 'K': '55.39', 'ROE': '104.32', 'ClosePrice': '46.20', 'BookValue': '24.53'}


有時間再來分析。



Python script:

import httplib2
import logging
import os
import re
import sys

class KTaiwanBankCrawler():

    def __init__(self, src_filepath):
        self._logger = logging.getLogger()
        self._src_filepath = src_filepath
     
    def Crawl(self):
        rv = []
        self._Phase0(rv)
        self._Phase1(rv)
        self._Phase2(rv)
        return rv      
     
    def _Phase0(self, rv):
        """
        Crawl stock symbol, ROE, and close price from a prepared source file.
        We need to copy the whole content from http://fund.bot.com.tw/z/index.htm
        to a single source file.
        """
        if not os.path.isfile(self._src_filepath): return None

        for line in open(self._src_filepath):
            line.strip()
            t = line.split()
            if len(t) is 7:
                stock_symbol = t[0][:4]
                if stock_symbol.isdigit():
                    info = { 'StockSymbol' : stock_symbol, 'ROE' : t[4], 'ClosePrice': t[1] }
                    rv.append(info)

    def _Phase1(self, rv):
        for info in rv:
            info = self._Phase1OneStock(info)

    def _Phase1OneStock(self, info):
        """
        Crawl book value from http://tw.stock.yahoo.com
        """
        bv = ''
        try:
            h = httplib2.Http()
            url = '''http://tw.stock.yahoo.com/d/s/company_%s.html''' % info['StockSymbol']
            resp, content = h.request(url)

            p = '每股淨值: \u3000\u3000'
            m = re.compile('.*' + p + '.*')

            lines = content.splitlines()
            for i in range(len(lines)):
                str_line = lines[i].decode('big5')
                if m.match(str_line):
                    begin_index = str_line.index(p) + len(p)
                    end_index = str_line.index('元', begin_index)
                    bv = str_line[begin_index:end_index]
        except: pass
        info['BookValue'] = bv
        return info

    def _Phase2(self, rv):
        for info in rv:
            try:
                ROE = float(info['ROE'])
                P = float(info['ClosePrice'])
                B = float(info['BookValue'])
                info['K'] = '''%.2f''' % ( ROE * B / P )
            except:
                info['K'] = None


def main():
    import argparse
    import base.logger
 
    base.logger.config_root(level=logging.DEBUG)

    parser = argparse.ArgumentParser(description='K Analyzer')
    parser.add_argument('src',  help='set stock filepath')
    args = parser.parse_args()

    c = KTaiwanBankCrawler(args.src)
    for rv in c.Crawl(): print(rv)
 
if __name__ == '__main__':
    sys.exit(main())

沒有留言:

張貼留言