How to scrape stock prices of individual stocks from the Nikkei newspaper website with Python

I wanted to analyze the stock price and find the stock at the time of purchase, so I scraped it from the Nikkei newspaper site. There are various sites that offer stock prices for free, such as Stock Investment Memo, but they are often updated irregularly. The Nikkei newspaper site is updated daily.

First of all, when scraping a site, if you do not follow various rules, it will be a crime, but the Nikkei newspaper [robots.txt]( As far as https://www.nikkei.com/robots.txt) and Terms of Service are seen, there seems to be no problem within the scope of personal use (? )is. (Please let me know if it doesn't work)

If you use the method read_html () of the module called pandas, it will take seconds.

nikkei_scrape.py


import pandas as pd

def get_stock_prices(stock_number):
    url = "https://www.nikkei.com/nkd/company/history/dprice/?scode={}&ba=1".format(stock_number)
    headers = {
        "User-Agent": "User-Agent information"
    }
    dfs = pd.read_html(url)
    for i in range(len(dfs)):
        if "date" in str(dfs[i]):
            return dfs[i]
    return False

User-Agent information is okay if you copy and paste the character string that appears when you open this site. For example, in my case, it was Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 81.0.4044.122 Safari / 537.36. For more information, click here [https://qiita.com/nightyknite/items/b2590a69f2e0135756dc).

Enter the 4-digit stock code in stock_number. For example, if you enter 1301, it is the URL of the stock price page of the company Kyokuyo [https://www.nikkei.com/nkd/company/history/dprice/?scode=1301&ba=1](https://www.nikkei. Scraping com / nkd / company / history / dprice /? Scode = 1301 & ba = 1). You can easily find the brand code by google, and you can also find it on the TSE website. You can download the EXCEL file.

There may be an extra table in the stock price web page, so to get only the stock price table, I turn the for statement to return only the table that contains the word "date".

that's all.

Recommended Posts

How to scrape stock prices of individual stocks from the Nikkei newspaper website with Python
How to scrape image data from flickr with python
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
How to crop the lower right part of the image with Python OpenCV
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to specify attributes with Mock of python
Note: How to get the last day of the month with python (added the first day of the month)
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to correctly upgrade the software when building Linux (CentOS) with Vagrant ~ Using the example of upgrading from Python 2.7 to Python 3.6 ~
[Python] How to remove duplicate values from the list
The wall of changing the Django service from Python 2.7 to Python 3
How to get the number of digits in Python
Learn Nim with Python (from the beginning of the year).
[Python] How to specify the download location with youtube-dl
[Python] Summary of how to specify the color of the figure
[Python] How to rewrite the table style with python-pptx [python-pptx]
From the initial state of CentOS8 to running php python perl ruby with nginx
Extract images and tables from pdf with python to reduce the burden of reporting
How to count the number of occurrences of each element in the list in Python with weight
The 15th offline real-time I tried to solve the problem of how to write with python
How to return to the command from the state where you can not enter interactive mode with python of git bash
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
How to write offline real time I tried to solve the problem of F02 with Python
I evaluated the strategy of stock system trading with Python.
How to quickly count the frequency of appearance of characters from a character string in Python?
Try to automate the operation of network devices with Python
How to scrape at speed per second with Python Selenium
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
Get the source of the page to load infinitely with python.
I want to extract an arbitrary URL from the character string of the html source with python
Try to calculate the position of the transmitter from the radio wave propagation model with python [Wi-Fi, Beacon]
How to determine the existence of a selenium element in Python
How to change the log level of Azure SDK for Python
[Introduction to Python] How to split a character string with the split function
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
How to get followers and followers from python using the Mastodon API
[Python] How to get the first and last days of the month
I want to output the beginning of the next month with Python
Output the contents of ~ .xlsx in the folder to HTML with Python
How to avoid duplication of data when inputting from Python to SQLite.
How to check the memory size of a dictionary in Python
How to connect to Cloud Firestore from Google Cloud Functions with python code
[Python] Explains how to use the format function with an example
How to monitor the execution status of sqlldr with the pv command
How to update the python version of Cloud Shell on GCP
I tried to improve the efficiency of daily work with Python
How to send a request to the DMM (FANZA) API with python
PhytoMine-I tried to get the genetic information of plants with Python
Python: How to use async with
Create folders from '01' to '12' with python
Existence from the viewpoint of Python
How to get the Python version
How to get started with Python
[Python] How to import the library
How to use FTP with Python