Download Japanese stock price data with python

Summary

I wrote a script to download stock price information from Stock Investment Memo without scraping.

how to use

python ./stockDownload.py -c 7203

7203 Toyota Motor Corporation's 2019 daily data can be downloaded as csv. If the download is successful, it returns Code: 7203 download finished., and if it fails, it returns Code: not valid..

Motivation

Scraping is prohibited from Yahoo! finance. The method of scraping stock price information from Stock Investment Memo was disclosed [^ 1], but the format may be changed and parsing may not work. On the other hand, there is a download button on the site, so I was investigating whether I could make good use of it.

How the download is done

After pressing the download button, I analyzed it from the network tab of the google developer tool. It seems that data is POSTed to https://kabuoji3.com/stock/file.php.

kabuoji3_POST_edit.png

Parts that need adjustment

--Since the script's fullName is the save destination, change it as appropriate. --If there is no header, a 403 error will occur, so check user-agent with the google developer tool. [^ 2] --sleep (3) is included to avoid excessive server load.

script

{stockDownload.py}


#!/usr/bin/env python

import requests
import re
import click
from time import sleep

@click.command()
@click.option("--code", "-c", "code", required=True,
        help="Stock code to download.")
def main(code):
    year = "2019"
    session = requests.Session()
    headers = {
                "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"
                }
    data = {
            "code":code,
            "year":year,
            "csv":""
            }
    url = "https://kabuoji3.com/stock/file.php"
    res = session.post(url, data=data, headers=headers)
    try:
        contentDisposition = res.headers['Content-Disposition']
        fileName = re.findall(r'\"(.+?)\"', contentDisposition)[0]
        fullName = ~/Documents/projects/ipo/data/stock/{}".format(fileName)
        with open(fullName, "wb") as saveFile:
                saveFile.write(res.content)
        print("Code: {} download finished.".format(code))
    except KeyError:
        print("Code: {} not valid.".format(code))
    sleep(3)

if __name__ == '__main__':
    main()

Feelings

I created a cli using click for the first time. I think it's easier to read than sys.argv. All you have to do is use shell's cat code | while read line: do python ./stockDownload.py -c $ line; done. Since it is cp932 encoded, it needs to be converted as nkf.

Reference material

[Python] Pseudo-click the button with requests How to Write Python Command-Line Interfaces like a Pro

[^ 1]: [Python] Get stock price data by scraping [^ 2]: [Python] What to do when scraping 403 Forbidden: You do n’t have permission to access on this server

Recommended Posts

Download Japanese stock price data with python
Get stock price data with Quandl API [Python]
Get stock price with Python
Generate Japanese test data with Python faker
Data analysis with Python
Automatic acquisition of stock price data with docker-compose
Let's do web scraping with Python (stock price)
Sample data created with python
Get Youtube data with python
Data is missing when getting stock price data with Pandas-datareader
Stock price forecast with tensorflow
Japanese morphological analysis with Python
Python: Stock Price Forecast Part 2
[Time series with plotly] Dynamic visualization with plotly [python, stock price]
Stock price data acquisition tips
Download csv file with python
Get the stock price of a Japanese company with Python and make a graph
Python: Stock Price Forecast Part 1
Read json data with python
"Getting stock price time series data from k-db.com with Python" Program environment creation memo
Programming history 1 month Extract NY Dow stock price with Python!
[Stock price analysis] Learning pandas with fictitious data (002: Log output)
Get Japanese stock price information from yahoo finance with pandas
Implemented file download with Python + Bottle
[Python] Get economic data with DataReader
[Python] My stock price forecast [HFT]
Python data structures learned with chemoinformatics
Speak Japanese text with OpenJTalk + python
Easy data visualization with Python seaborn.
Process Pubmed .xml data with python
Data analysis starting with python (data visualization 1)
Data analysis starting with python (data visualization 2)
Python application: Data cleansing # 2: Data cleansing with DataFrame
[Stock price analysis] Learning pandas with fictitious data (001: environment preparation-file reading)
Download python
[Stock price analysis] Learning pandas with fictitious data (003: Type organization-candlestick chart)
Get US stock price from Python with Web API with Raspberry Pi
[In-Database Python Analysis Tutorial with SQL Server 2017] Step 1: Download sample data
Get additional data in LDAP with python
Data pipeline construction with Python and Luigi
[Note] Get data from PostgreSQL with Python
Read CSV file with python (Download & parse CSV file)
Retrieving food data with Amazon API (Python)
HTTP split download guy made with Python
Try working with binary data in Python
Convert Excel data to JSON with python
[Python] Use string data with scikit-learn SVM
Notes on doing Japanese OCR with Python
Check stock prices with slackbot using python
Manipulate DynamoDB data with Lambda (Node & Python)
Convert FX 1-minute data to 5-minute data with Python
Web scraping with Python ② (Actually scraping stock sites)
How to display python Japanese with lolipop
Download files on the web with Python
[Python] Let's make matplotlib compatible with Japanese
Stock Price Forecast with TensorFlow (LSTM) ~ Stock Forecast Part 1 ~
[Python] Creating a stock price drawdown chart
Recommendation of Altair! Data visualization with Python
Data analysis starting with python (data preprocessing-machine learning)
Let's do MySQL data manipulation with Python
How to enter Japanese with Python curses