Obtain the name and securities code of a stock listed on the First Section of the Tokyo Stock Exchange

Get the Excel file from the official page of Tokyo Stock Exchange and [pandas](http: / Store in /pandas.pydata.org/ "pandas"). By the way, let's save it as a CSV file.

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(), "Market Part 1 (domestic stocks)")]')[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"code"].astype('int64'), 'name': df[u"trading name"]})

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)

Recommended Posts

Obtain the name and securities code of a stock listed on the First Section of the Tokyo Stock Exchange
Let's take a look at the Scapy code. Overload of special methods __div__, __getitem__ and so on.
Get the stock price of a Japanese company with Python and make a graph
A tool to insert the country name and country code in the IP address part
Calculate the probability of outliers on a boxplot
A discussion of the strengths and weaknesses of Python
[Python3] Take a screenshot of a web page on the server and crop it further