Introduction to Python Web Scraping Practice Rewritten in Python 3 with reference to
$ python3 -V
Python 3.8.6
$ pip3 install beautifulsoup4
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = "http://www.nikkei.com/markets/kabu/"
html = urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
nikkei_heikin = soup.find("span", class_="mkc-stock_prices").string
print(nikkei_heikin)
$ pip3 install beautifulsoup4
$ pip3 install apscheduler
$ pip3 install requests
import csv
import datetime
import requests
from apscheduler.schedulers.blocking import BlockingScheduler
from bs4 import BeautifulSoup
sched = BlockingScheduler()
#Run every hour
# @sched.scheduled_job('interval', hours=1)
#Run at 0 minutes every hour
@sched.scheduled_job("cron", minute=0, hour="*/1")
def scheduled_job():
#Access the Nikkei Stock Average page of the Nihon Keizai Shimbun and get the HTML
r = requests.get("http://www.nikkei.com/markets/kabu/")
r.raise_for_status()
#Get the Nikkei Stock Average using Beautiful Soup
soup = BeautifulSoup(r.text, "html.parser")
nikkei_heikin = soup.select_one(
"#CONTENTS_MARROW > div.mk-top_stock_average.cmn-clearfix > div.cmn-clearfix > div.mkc-guidepost > div.mkc-prices > span.mkc-stock_prices"
).get_text(strip=True)
#Convert the current time to a string
now = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
print(f"{now} {nikkei_heikin}"
#Add the date and time and the Nikkei Stock Average to CSV
with open("nikkei_heikin.csv", "a") as fw:
writer = csv.writer(fw, dialect="excel", lineterminator="\n")
writer.writerow([now, nikkei_heikin])
sched.start()
Recommended Posts