Programming history 1 month Extract NY Dow stock price with Python!

Nice to meet you, my name is Kay.

Since I was investing, I felt a frontier in Python from January of this year, and I was wondering if it could be applied to investment, so I finally reached the point of extracting the stock price of Yahoo Finance. That's why I have been programming for a month (laughs) github↓ https://github.com/Kay-Hatsune/NY-Dow/blob/master/dow.py

What was used

Language: Python3

Library: urllib, BeautifulSoup

MacBook Pro

Install Beautiful Soup

shell.sh


$ pip3 install beautifulsoup

In my case, it was MacOS, so I installed pip3. Please note that people in windows are different.

Python code

dow.py


import urllib.request
import ssl
from bs4 import BeautifulSoup

url = "https://finance.yahoo.co.jp/quote/%5EDJI"

ssl._create_default_https_context = ssl._create_unverified_context

html = urllib.request.urlopen(url)

soup = BeautifulSoup(html, "html.parser")

p = soup.find_all("p")

dow = ""
for tag in p:
    try:
        string_ = tag.get("class").pop(0)

        if string_ in "wlbmIy9W":
            dow = tag.string
            break
    except:
        pass

print(dow)

Pull url from Yahoo Finance with Beautiful Soup. ↓ Since the stock price is in the place called p, search for p in the html data. ↓ Construct with a for statement and a try except statement to specify the location of class = "wlbmIy9W" in p.

By the way, you can easily identify the extraction location by right-clicking on the site and pressing Validate. スクリーンショット 2020-02-04 18.51.58.png

Note that Python 3 requires an SSL certificate

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

In Python3 you need to write this code. If you do not write it, an error will always occur. Maybe you don't need it in Python2.

Extraction completed

shell.sh


$ python dow.py

>>>28,399.81

The extraction is finally completed. It took 5 hours (laughs) I want to develop it further in the future. : stuck_out_tongue_winking_eye:

Articles that I used as a reference

This is a must read as it writes more advanced code!

Introduction to Python Web Scraping Practice

Recommended Posts

Programming history 1 month Extract NY Dow stock price with Python!
Get stock price with Python
Download Japanese stock price data with python
Get stock price data with Quandl API [Python]
Let's do web scraping with Python (stock price)
3. 3. AI programming with Python
Python programming with Atom
Competitive programming with python
Programming with Python Flask
[Time series with plotly] Dynamic visualization with plotly [python, stock price]
Programming with Python and Tkinter
Stock price forecast with tensorflow
Python: Stock Price Forecast Part 2
Network programming with Python Scapy
Python: Stock Price Forecast Part 1
Get US stock price from Python with Web API with Raspberry Pi
Extract the xz file with python
[Automation] Extract Outlook appointments with Python
[Python] Object-oriented programming learned with Pokemon
[Python] My stock price forecast [HFT]
Easy Python + OpenCV programming with Canopy
[Beginner] Extract character strings with Python
Competitive programming with python Local environment settings
Check stock prices with slackbot using python
Web scraping with Python ② (Actually scraping stock sites)
Stock Price Forecast with TensorFlow (LSTM) ~ Stock Forecast Part 1 ~
[Python] Creating a stock price drawdown chart
Get the stock price of a Japanese company with Python and make a graph