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
Language: Python3
Library: urllib, BeautifulSoup
MacBook Pro
shell.sh
$ pip3 install beautifulsoup
In my case, it was MacOS, so I installed pip3. Please note that people in windows are different.
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.
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.
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:
This is a must read as it writes more advanced code!
Introduction to Python Web Scraping Practice
Recommended Posts