Last active
July 12, 2018 08:09
-
-
Save umihico/fc5e17881390c6a3595eeb5acf3130bd to your computer and use it in GitHub Desktop.
yahooファイナンスから銘柄情報を取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gen_url(stock_code="8604"): | |
fundamental_url = f"https://profile.yahoo.co.jp/fundamental/{stock_code}" | |
consolidate_url = f"https://profile.yahoo.co.jp/consolidate/{stock_code}" | |
independent_url = f"https://profile.yahoo.co.jp/independent/{stock_code}" | |
urls = [fundamental_url, consolidate_url, independent_url] | |
return urls | |
def get_stock_info_yahoo(lxmltrees): | |
result = {} | |
result.update(_get_fundamental(lxmltrees[0])) | |
result.update(_get_consolidate(lxmltrees[1])) | |
result.update(_get_independent(lxmltrees[2])) | |
return result | |
def _get_fundamental(lxmltree): | |
update_date = lxmltree.xpath( | |
"//div[contains(text(),'最終更新日')]")[0].text_content() | |
trs = lxmltree.xpath( | |
"//div[.//h1[@class='pro_title2']]//table//tr") | |
result = {"最終更新日": update_date} | |
for tr in trs: | |
for td in tr.xpath(".//td"): | |
if "bgcolor" in td.attrib: | |
key = td.text_content() | |
else: | |
value = td.text_content() | |
try: | |
result[key] = value | |
except (Exception, ) as e: | |
pass | |
return result | |
def _get_consolidate(lxmltree): | |
xpath = "//table[.//tr/td[text()='決算期']]" | |
table = lxmltree.xpath(xpath)[1] | |
rows = [] | |
for tr in table.xpath(".//tr"): | |
row = [td.text_content() for td in tr.xpath(".//td")] | |
rows.append(row) | |
horizonal_field_names = rows[0][1:] | |
# print(rows[0][1:]) | |
vertical_field_names = [row[0] for row in rows[1:]] | |
# print(vertical_field_names) | |
result = {} | |
for vertical_field_name, row in zip(vertical_field_names, rows[1:]): | |
for horizonal_field_name, col in zip(horizonal_field_names, row[1:]): | |
result[horizonal_field_name + ":" + vertical_field_name] = col | |
return result | |
def _get_independent(lxmltree): | |
result = _get_consolidate(lxmltree) | |
result = {'(単独)' + key: value for key, value in result.items()} | |
return result | |
if __name__ == '__main__': | |
from lxml import html | |
from requests import get | |
from umihico_commons.functools import save_as_txt | |
urls = gen_url(stock_code="8604") | |
lxmltrees = [html.fromstring(get(url).text) for url in urls] | |
result = get_stock_info_yahoo(lxmltrees) | |
save_as_txt("result.txt", result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{'(単独)2期前:1株配当': '20.00円', | |
'(単独)2期前:BPS(一株当たり純資産)': '---', | |
'(単独)2期前:EPS(一株当たり利益)': '---', | |
'(単独)2期前:ROA(総資産利益率)': '---', | |
'(単独)2期前:ROE(自己資本利益率)': '---', | |
'(単独)2期前:会計方式': 'SEC', | |
'(単独)2期前:営業利益': '---', | |
'(単独)2期前:売上高': '---', | |
'(単独)2期前:当期利益': '---', | |
'(単独)2期前:有利子負債': '---', | |
'(単独)2期前:決算月数': '12か月', | |
'(単独)2期前:決算期': '2017年3月期', | |
'(単独)2期前:決算発表日': '2017年4月27日', | |
'(単独)2期前:発行済み株式総数': '3,822,562千株', | |
'(単独)2期前:経常利益': '---', | |
'(単独)2期前:総資産': '---', | |
'(単独)2期前:総資産経常利益率': '---', | |
'(単独)2期前:自己資本': '---', | |
'(単独)2期前:自己資本比率': '---', | |
'(単独)2期前:調整一株当たり利益': '---', | |
'(単独)2期前:資本金': '---', | |
'(単独)2期前:配当区分': '---', | |
'(単独)3期前:1株配当': '13.00円', | |
'(単独)3期前:BPS(一株当たり純資産)': '---', | |
'(単独)3期前:EPS(一株当たり利益)': '116.47円', | |
'(単独)3期前:ROA(総資産利益率)': '5.98%', | |
'(単独)3期前:ROE(自己資本利益率)': '18.97%', | |
'(単独)3期前:会計方式': 'SEC', | |
'(単独)3期前:営業利益': '357,645百万円', | |
'(単独)3期前:売上高': '585,394百万円', | |
'(単独)3期前:当期利益': '419,507百万円', | |
'(単独)3期前:有利子負債': '---', | |
'(単独)3期前:決算月数': '12か月', | |
'(単独)3期前:決算期': '2016年3月期', | |
'(単独)3期前:決算発表日': '2016年4月27日', | |
'(単独)3期前:発行済み株式総数': '3,822,562千株', | |
'(単独)3期前:経常利益': '353,058百万円', | |
'(単独)3期前:総資産': '7,379,122百万円', | |
'(単独)3期前:総資産経常利益率': '5.04%', | |
'(単独)3期前:自己資本': '2,373,256百万円', | |
'(単独)3期前:自己資本比率': '32.2%', | |
'(単独)3期前:調整一株当たり利益': '113.33円', | |
'(単独)3期前:資本金': '---', | |
'(単独)3期前:配当区分': '---', | |
'(単独)前期:1株配当': '20.00円', | |
'(単独)前期:BPS(一株当たり純資産)': '---', | |
'(単独)前期:EPS(一株当たり利益)': '---', | |
'(単独)前期:ROA(総資産利益率)': '---', | |
'(単独)前期:ROE(自己資本利益率)': '---', | |
'(単独)前期:会計方式': 'SEC', | |
'(単独)前期:営業利益': '---', | |
'(単独)前期:売上高': '---', | |
'(単独)前期:当期利益': '---', | |
'(単独)前期:有利子負債': '---', | |
'(単独)前期:決算月数': '12か月', | |
'(単独)前期:決算期': '2018年3月期', | |
'(単独)前期:決算発表日': '2018年4月26日', | |
'(単独)前期:発行済み株式総数': '3,643,562千株', | |
'(単独)前期:経常利益': '---', | |
'(単独)前期:総資産': '---', | |
'(単独)前期:総資産経常利益率': '---', | |
'(単独)前期:自己資本': '---', | |
'(単独)前期:自己資本比率': '---', | |
'(単独)前期:調整一株当たり利益': '---', | |
'(単独)前期:資本金': '---', | |
'(単独)前期:配当区分': '---', | |
'2期前:BPS(一株当たり純資産)': '790.70円', | |
'2期前:EPS(一株当たり利益)': '67.29円', | |
'2期前:ROA(総資産利益率)': '0.57%', | |
'2期前:ROE(自己資本利益率)': '8.73%', | |
'2期前:会計方式': 'SEC', | |
'2期前:営業利益': '‥百万円', | |
'2期前:売上高': '1,715,516百万円', | |
'2期前:当期利益': '239,617百万円', | |
'2期前:有利子負債': '8,076,526百万円', | |
'2期前:決算月数': '12か月', | |
'2期前:決算期': '2017年3月期', | |
'2期前:決算発表日': '2017年4月27日', | |
'2期前:経常利益': '322,795百万円', | |
'2期前:総資産': '42,852,078百万円', | |
'2期前:総資産経常利益率': '0.77%', | |
'2期前:自己資本': '2,789,916百万円', | |
'2期前:自己資本比率': '6.5%', | |
'2期前:調整一株当たり利益': '65.65円', | |
'2期前:資本金': '594,493百万円', | |
'3期前:BPS(一株当たり純資産)': '748.32円', | |
'3期前:EPS(一株当たり利益)': '36.53円', | |
'3期前:ROA(総資産利益率)': '0.32%', | |
'3期前:ROE(自己資本利益率)': '4.87%', | |
'3期前:会計方式': 'SEC', | |
'3期前:営業利益': '‥百万円', | |
'3期前:売上高': '1,723,096百万円', | |
'3期前:当期利益': '131,550百万円', | |
'3期前:有利子負債': '9,268,734百万円', | |
'3期前:決算月数': '12か月', | |
'3期前:決算期': '2016年3月期', | |
'3期前:決算発表日': '2016年4月27日', | |
'3期前:経常利益': '165,158百万円', | |
'3期前:総資産': '41,090,167百万円', | |
'3期前:総資産経常利益率': '0.40%', | |
'3期前:自己資本': '2,700,239百万円', | |
'3期前:自己資本比率': '6.6%', | |
'3期前:調整一株当たり利益': '35.52円', | |
'3期前:資本金': '594,493百万円', | |
'上場年月日': '1961年10月', | |
'代表者名': '\n永井\u3000浩二', | |
'前期:BPS(一株当たり純資産)': '810.31円', | |
'前期:EPS(一株当たり利益)': '63.13円', | |
'前期:ROA(総資産利益率)': '0.53%', | |
'前期:ROE(自己資本利益率)': '7.92%', | |
'前期:会計方式': 'SEC', | |
'前期:営業利益': '‥百万円', | |
'前期:売上高': '1,972,158百万円', | |
'前期:当期利益': '219,343百万円', | |
'前期:有利子負債': '8,539,625百万円', | |
'前期:決算月数': '12か月', | |
'前期:決算期': '2018年3月期', | |
'前期:決算発表日': '2018年4月26日', | |
'前期:経常利益': '328,158百万円', | |
'前期:総資産': '40,591,329百万円', | |
'前期:総資産経常利益率': '0.79%', | |
'前期:自己資本': '2,749,320百万円', | |
'前期:自己資本比率': '6.8%', | |
'前期:調整一株当たり利益': '61.88円', | |
'前期:資本金': '594,493百万円', | |
'単元株数': '100株', | |
'市場名': '東証1部,名証1部', | |
'平均年収': ' 【ご注意】\n' | |
'この情報は投資判断の参考としての情報を目的としたものであり、投資勧誘を目的としたものではありません。\n' | |
'提供している情報の内容に関しては万全を期しておりますが、その内容を保証するものではありません。\n' | |
'万一この情報に基づいて被ったいかなる損害についても、当社および情報提供元は一切責任を負いかねます。', | |
'平均年齢': '‐歳', | |
'従業員数(単独)': '-人', | |
'従業員数(連結)': '28,048人', | |
'最寄り駅': '\n 〜 日本橋(東京都)\n\n\n\n', | |
'最終更新日': '最終更新日:2018年7月12日', | |
'本社所在地': '〒103-8645 東京都中央区日本橋1−9−1 [周辺地図]', | |
'業種分類': '証券業', | |
'決算': '3月末日', | |
'特色': '証券国内最大手。海外は欧米で体制再構築の一方、アジアに力点。国内はコンサル重視営業推進', | |
'英文社名': 'Nomura\u3000Holdings,\u3000Inc.', | |
'設立年月日': '1925年12月25日', | |
'連結事業': '【連結事業】受入手数料37、トレーディング損益22、金融収益30、他11(2018.3)', | |
'電話番号': '03−5255−1000'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment