昨日、カルロ-スゴーンさんとコメントのやり取りをしていて、そういや適時開示のデータを取ってみようと思っていたのですが、すっかり忘れていたことを思い出しました。
しかし、XRBLのパースとかは私には荷が重すぎるので、一先ずタイトルを取得し、月次データなんかを認識して時間、銘柄コード、銘柄名、タイトル、リンク先を残すクローラーを作ってみました。
普通のアドレスだとデータが取れなかった
適時開示のサイトから普通にデータを取ろうとしたら、全く何も取れませんでした。なんやこれ・・・
と思って、リンクを見ているとどうも違うアドレスがあったので、それを取得先にすると取れました。良かった良かった。
アドレスは以下のような感じでした。
[https://www.release.tdnet.info/inbs/I_list_001_20181015.html]
最後の部分が日付で、その前の001がページ数で、001が最も新しいもの、その後のページは普通に数字を増やしていくという感じのアドレスのつくりになってるのかなぁって感じなので、そんな感じでアドレスを作るようにしました。
その日のページが何ページあるかというのをどうするかというのもどう処理するかぁという風に思いましたが、現時点の件数があるので、一先ずそれを100で割って、ページ数を予想して全部のページをクロールです。
以上の設計は引けてから一回でデータを取ろうかなぁという感じの設計です。どうせザラ場データをとっても間に合いませんしねぇ。
というわけで作ってみた
部品的には日付を取るもの、その日のURLのトップを取るもの、そこからその日の件数を取るもの、それらを一括に取るもの、それらを使ってデータを集めるもの見たいな時にしました。その日のやつが取れたらええなーと思いましたが、以前他のやつでそんな感じにして、あー違う日が取りたいってなって、あとから直す大変さを痛感したので、最初から分けておきました。
で、データを取る時にタイトルから取捨選択するようにしました。現状は、一部指定、月次、修正を含むタイトルを持つデータをピックアップするようにしました。
今後の展開
PDF開くのってめんどくさい。ので、PDFを開いて画像をどこかで一括で見られるというのを作ろうかと思っています。
活字のサイズとかって結構決まってるし、機械学習させやすいのではとか思っている。代わりに読んでほしい。どうせなら今月末までにそのひな形を作っておきたい。
Pythonをはじめたい人にお勧め!
- 作者: 柴田淳
- 出版社/メーカー: SBクリエイティブ
- 発売日: 2016/12/22
- メディア: 単行本
- この商品を含むブログ (2件) を見る
Pythonでデータ分析したい人にお勧め!Pandasの作者による著作
Pythonによるデータ分析入門 第2版 ―NumPy、pandasを使ったデータ処理
- 作者: Wes McKinney,瀬戸山雅人,小林儀匡,滝口開資
- 出版社/メーカー: オライリージャパン
- 発売日: 2018/07/26
- メディア: 単行本(ソフトカバー)
- この商品を含むブログを見る
コード
import re import time from datetime import datetime from requests_html import HTMLSession # 日付取得 def get_date(date=None): if date: if len(date) == 8: return date else: print('Please enter date / 8 digits!') else: t = datetime.now() if len(str(t.month)): month1 = str(t.month) else: month1 = '0' + str(t.month) t1 = '{}{}{}'.format(str(t.year), month1, str(t.day)) return t1 # URL取得 def get_url(date=None): if date: url = 'https://www.release.tdnet.info/inbs/I_list_001_{}.html'.format(date) else: t = datetime.now() if len(str(t.month)): month1 = str(t.month) else: month1 = '0' + str(t.month) t1 = '{}{}{}'.format(str(t.year), month1, str(t.day)) url = 'https://www.release.tdnet.info/inbs/I_list_001_{}.html'.format(t1) return url # 件数取得 def get_url_cases(url): session = HTMLSession() r = session.get(url) r.html.render() kensus = r.html.find('#pager-box-top > div.kaijiSum') for kensu in kensus: cases = kensu.text.split('/')[1].replace('全', '').replace('件', '') return int(cases) # 日付、URL、件数まとめて取得 def get_date_url_cases(date=None): date1 = get_date(date) url = get_url(date1) cases = get_url_cases(url) return date1, url, cases # データ取得 def scat_datas(date, cases): ichibu_list = list() getsuji_list = list() syusei_list = list() case = cases // 100 + 1 for i in range(1, case): if len(str(i)) == 1: site_url = 'https://www.release.tdnet.info/inbs/I_list_00{}_{}.html'.format(i, date) elif len(str(i)) == 2: site_url = 'https://www.release.tdnet.info/inbs/I_list_0{}_{}.html'.format(i, date) else: site_url = 'https://www.release.tdnet.info/inbs/I_list_{}_{}.html'.format(i, date) session = HTMLSession() r = session.get(site_url) r.html.render() table_datas = r.html.find('#main-list') length = len(table_datas[0].find('tr')) for dan in range(1, length): if dan % 2 == 0: c_time = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.evennew-L.kjTime'.format(dan)) c_code = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.evennew-M.kjCode'.format(dan)) c_name = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.evennew-M.kjName'.format(dan)) c_title = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.evennew-M.kjTitle > a'.format(dan)) c_link = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.evennew-M.kjTitle > a'.format(dan)) else: c_time = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.oddnew-L.kjTime'.format(dan)) c_code = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.oddnew-M.kjCode'.format(dan)) c_name = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.oddnew-M.kjName'.format(dan)) c_title = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.oddnew-M.kjTitle > a'.format(dan)) c_link = table_datas[0].find('#main-list-table > tbody > tr:nth-child({}) > td.oddnew-M.kjTitle > a'.format(dan)) try: a_time = c_time[0].text code = c_code[0].text name = c_name[0].text title = c_title[0].text links = c_link[0].absolute_links except: a_time = '' code = '' name = '' title = '' links = '' ichibu = re.compile('第一部指定') getsuji = re.compile('月次') syusei = re.compile('修正') result_ichibu = ichibu.search(title) result_getsuji = getsuji.search(title) result_syusei = syusei.search(title) if result_ichibu: for link in links: ichibu_list.append([date, a_time, code, name, title, link]) elif result_getsuji: for link in links: getsuji_list.append([date, a_time, code, name, title, link]) elif result_syusei: for link in links: syusei_list.append([date, a_time, code, name, title, link]) time.sleep(5) return ichibu_list, getsuji_list, syusei_list if __name__ == '__main__': date, url, cases = get_date_url_cases(date='20181012') ichibu, getsuji,syusei = scat_datas(date, cases) print(getsuji)