[PYTHON] Display TOPIX time series

What to do

Plot the TOPIX time series of Yahoo Finance and observe the changes in the economy

What package to use

urllib2 Make an http request and get html

urllib2.urlopen(url).read()

lxml xml, html parser Suppose the variable html contains the following html string

<table>
<tr><td>aa</td><td>bb</td></tr>
<tr><td>aa</td><td>bb</td></tr>
<tr><td>aa</td><td>bb</td></tr>
</tr>

You can display the contents of all td with the following code.

root  = lxml.html.fromstring(html)
root.xpath("//table")
for tr in root.xpath("descendant::tr"):
    for td in root.xpath("descendant::td"):
         print td

code

import pylab
import urllib2
import lxml
import lxml.html
import re

dateFr = {"year": 2000, "month":1, "day":1}
dateTo = {"year": 2013, "month":11, "day": 1}

data = []
for page in range(1, 30):
    print page
    url = "http://info.finance.yahoo.co.jp/history/?code=998405.T&sy=%d&sm=%d&sd=%d&ey=%d&em=%d&ed=%d&tm=d&p=%d"
    url = url % (dateFr["year"], dateFr["month"], dateFr["day"], dateTo["year"], dateFr["month"], dateFr["day"], page)

    html = urllib2.urlopen(url).read()
    root = lxml.html.fromstring(html)
    table = root.xpath('//*[contains(concat(" ",normalize-space(@class)," "), " boardFin ")]')[0]

    for tr in table.xpath("descendant::tr"):
        tmp = [td.text for td in tr.xpath("descendant::td")]
        if len(tmp) != 5:
            continue
        begin = float(tmp[1].replace(",", ""))
        high  = float(tmp[2].replace(",", ""))
        low   = float(tmp[3].replace(",", ""))
        end   = float(tmp[4].replace(",", ""))
        data.append([low, high, low, high])
pylab.boxplot(data)
pylab.show()

image

topix.png

Recommended Posts

Display TOPIX time series
Time Series Decomposition
Python: Time Series Analysis
Python time series question
RNN_LSTM1 Time series analysis
Time series analysis 1 Basics
Time series plot / Matplotlib
Save TOPIX time series in pickle, csv, Excel format
Time series analysis related memo
Time series analysis part 4 VAR
Time series analysis Part 3 Forecast
[Python] Plot time series data
Time series analysis Part 1 Autocorrelation
Calculation of time series customer loyalty
Easy time series prediction with Prophet
Python: Time Series Analysis: Preprocessing Time Series Data
Time series analysis practice sales forecast
About time series data and overfitting
Differentiation of time series data (discrete)
Movement statistics for time series forecasting
LSTM (1) for time series forecasting (for beginners)
Forecasting time series data with Simplex Projection
Time series analysis 2 Stationary, ARMA / ARIMA model
Predict time series data with neural network
Display Disney's waiting time with LINE bot
I tried time series analysis! (AR model)
Time series analysis Part 2 AR / MA / ARMA
[Python] Accelerates loading of time series CSV
Time series analysis 4 Construction of SARIMA model
Time series data anomaly detection for beginners
matplotlib Write text to time series graph
How to handle time series data (implementation)
Reading OpenFOAM time series data and sets data
Time series analysis # 6 Spurious regression and cointegration
Format and display time series data with different scales and units with Python or Matplotlib