スキップしてメイン コンテンツに移動

[Python]日経225構成銘柄の一覧を取得するプログラム

今日書いたのはこれ。

日経225の構成銘柄が変更になるといちいち変更が面倒なので作りました。
コメントに書いてある機能しかありませんが、簡単でよし。

ご参考になれば。

#!/usr/local/bin/python
# -*- coding:utf-8 -*-

from BeautifulSoup import BeautifulSoup
import urllib2,re

class Nikkei225Profile(object):
    def __init__(self):
        '''
        日経新聞のサイトから日経225の構成銘柄の証券コードと証券名称を取得
        '''
        self.url = 'http://www3.nikkei.co.jp/nkave/about/225_list.cfm'
        self.profile = dict()
        soup = BeautifulSoup(urllib2.urlopen(self.url))
        tablesoup = soup.find("table")
        rows = tablesoup.findAll('tr',{'bgcolor':'#FFF5DE'})
        rows += tablesoup.findAll('tr',{'bgcolor':'#F0E7D1'})
        for row in rows:
            row_list = [cell.find(text=True) for cell in row.findAll('td')]
            self.profile[row_list[0]] = row_list[1]
    def getprofile(self,googlestyle=False):
        '''
        日経225の証券コードと証券名称をdict()で返す
        引数:googlestyleでGoogleFinanceの書式か否かを判定
        GoogleFinance書式は証券市場コード'TYO:'を証券コードの先頭に付加
        '''
        out = dict()
        if googlestyle==True:
            for k,v in self.profile.items():
                out[u'TYO:'+k] = v
            return out
        else:
            return self.profile
    def gettickers(self,googlestyle=False):
        '''
        日経225の証券コードをlist()で返す
        引数:googlestyleでGoogleFinanceの書式か否かを判定
        GoogleFinance書式は証券市場コード'TYO:'を証券コードの先頭に付加
        '''
        if googlestyle==True:
            return [u'TYO:'+ticker for ticker in self.profile.keys()]
        else:
            return self.profile.keys()

if __name__ == '__main__':
    n = Nikkei225Profile()
    print n.getprofile(googlestyle=True)
    print n.getprofile(googlestyle=False)
    print n.gettickers(googlestyle=True)
    print n.gettickers(googlestyle=False)

コメント

このブログの人気の投稿

[Python]redis-pyでRedis Pub/Sub実装

前から面白そうと思っていたRedisのPub/Sub機能。 redis-pyでどう実装すれば使えるか確認してみた。 ■Pub/Subについて http://ja.wikipedia.org/wiki/出版-購読型モデル ■pub.py from redis import StrictRedis def publish(channel,msg): """ redis-pyにはPUBLISHするためのメソッドがないので、 Redisのコマンドをそのまま実行する為のクラスを使う。 """ sr=StrictRedis() """ 第1引数にRedisのコマンド、第2引数以降は そのコマンドの引数をそのままセット """ sr.execute_command("PUBLISH",channel,msg) if __name__=="__main__": # チャネル"hoge"に"Hello"というメッセージを出版(Publish) publish("hoge","Hello") ■sub.py from redis import Redis def listen(channel): r=Redis() ps=r.pubsub() ps.subscribe(channel) while True: for i in ps.listen(): print i["data"] if __name__=="__main__": # チャネル"hoge"を購読(Subscribe) listen("hoge") ■実行方法・実行結果 (ターミナル1-準備) [user@localhost ~]# python sub.py 1 # => チャネ

[OS]VirtualBoxでCentOS6.5(x86_64)がKernel panicを起こした

完全に対処療法。 恥ずかしながら自分では原因がわかっていない。 忘れないようにメモしておく。 いろいろ設定したあと、何度目かの再起動のタイミングでKernel panicが発生して起動しないという事象が発生。 こちらの方法でなんとか起動させて。 [CentOS] SELinux 無効化後のカーネルパニック http://kuni255.blogspot.jp/2013/04/centos-selinux.html こちらの通り、/boot/grub/grub.confに設定を追加。 Kernel panic – not syncing: Attempted to kill init! http://h2np.net/mynotebook/post/130 設定変更後、以下の操作をそれぞれ10回ほど試した限りでは再発していません。  (1)halt+起動 (2)reboot 追記:と思ったらまた発生した。なんかもう解決できる気がしない。

[Python]システムトレード環境

最近、システムトレードをしている。 日本株は、自動売買までは実現できていないが、トレンド分析くらいはできるようになっている。 とりあえず、現時点の環境を整理しておきたい。 ■日本株 H/W:さくらVPS(2GBだったかな?) OS:CentOS6.4(x86_64) 言語:Python DB:SQLite3 データソース: http://www.data-get.com/main/ (有料) データ種別:株価データ、銘柄情報データ、分割併合データ ■FX H/W:DELL T100(6年前のもの)+CPU4コアに換装済+メモリ8GB化済 OS:Windows 7 64bit プラットフォーム:MetatTrader 4 言語:MQL4 取引業者:Alpari Japan ■クライアント H/W:MacBook Pro 13インチ(2010-Mid) 特に不満はないけど、さくらVPSが高い。 自宅のT100は古いけど重い処理をさせるわけじゃない。 そのうちさくらVPSから自宅PCのVirtualBoxかなんかの仮想環境に引っ越す可能性もある。 こんなところ。