34
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

東証1部上場銘柄の名前と証券コードを取得する

Posted at

東京証券取引所の公式ページから,Excelファイルを取得し,pandasに格納します.
ついでにCSVファイルに保存もしておきましょうかね.

getStockNameYJ.py
# -*- coding: utf-8 -*-
from urllib2 import *
from lxml import html
import numpy as np
import pandas as pd

base_url = 'http://www.tse.or.jp'

def getTosyo1DataURI():
	contents = urlopen(base_url + '/market/data/listed_companies/index.html').read()
	dom = html.fromstring(contents)

	ep = dom.xpath(u'.//td[contains(text(), "市場第一部 (内国株) ")]')[0].getparent()
	e = ep.xpath('.//a')[0]

	return base_url + e.attrib['href']

def getStockNameDF():
	ds = np.DataSource(None)
	f = ds.open(getTosyo1DataURI())
	df = pd.ExcelFile(f).parse('Sheet1')
	f.close()

	return pd.DataFrame({'code': df[u"コード"].astype('int64'), 'name': df[u"銘柄名"]})

def saveCSV(df):
	df[['code','name']].to_csv('tosyo1.csv', index=False, encoding='utf-8')

if __name__ == '__main__':
	df = getStockNameDF()
	#df.save('stock_name.pdpy')
	#df = pd.DataFrame.load('stock_name.pdpy')
	saveCSV(df)
34
33
8

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?