決算発表銘柄のチェック方法
「SBI証券ログイン」⇒「マーケット」⇒「経済カレンダー」⇒「決算発表スケジュール」
スクレイピング対象のサイトとロボットテキスト
私はSBI証券を利用しているため、上記のサイトではログインが必要ですし、以下の日本経済新聞をスクレイピングしたいと思います。
〇決算発表スケジュール :企業業績・財務 :マーケット :日経電子版 (nikkei.com)
とはいえ、本当にスクレイピングしてよいのかどうかを調べる必要があります。
〇https://www.nikkei.com/robots.txt
もちろん、相手側のサーバーに負荷がかかるようなことはしてはいけませんが、スクレイピングはOKのようです。
プログラムの構築
〇決算発表スケジュール :企業業績・財務 :マーケット :日経電子版 (nikkei.com)
日付を指定して、検索ボタンをクリックするとURLが変更されます。
そのURLを変数urlに格納してください。
そして、以下のプログラムを実行すると銘柄コード一覧がlistに格納されています。
import time
from bs4 import BeautifulSoup
import requests
headers = {"ヘッダー情報は任意で設定"}
import re
url = "https://www.nikkei.com/markets/kigyo/money-schedule/kessan/?ResultFlag=1&SearchDate1=2021%E5%B9%B411&SearchDate2=01"
print(url)
res = requests.get(url,headers=headers,timeout=3)
time.sleep(3)
soup = BeautifulSoup(res.text,"html.parser")
print(soup.title)
code = []
for i in range(1,51):
select = "#newpresSchedule > div.m-block.newpresSearchResults > div > div.m-artcle > table > tbody > tr:nth-child("+str(i)+") > td:nth-child(2) > a"
aaa = str(soup.select(select)[0].string)
code.append(aaa)
print(code)
しかし、これだけでは決算発表が51以上だと全てをlistに格納できていませんので、上記のコード新たにプログラムを追記していきます。
seleniumについての詳しい説明は以下のサイトがすごくわかりやすくてオススメです。
〇図解!PythonでSeleniumを使ったスクレイピングを徹底解説!(インストール・使い方・Chrome)
結果的にrequestsモジュールをを使わなくても、スクレイピングできますが、完成コード以下のようになりました。
url = "https://www.nikkei.com/markets/kigyo/money-schedule/kessan/?ResultFlag=1&SearchDate1=2021%E5%B9%B410&SearchDate2=01"
day = "2021-10-01"
import time
from bs4 import BeautifulSoup
import requests
headers = {""}
import re
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install()) #ヘッドレスモードではないときにアクティブにして下3行はコメントアウト
# options = webdriver.ChromeOptions()#ヘッドレスモードのときにアクティブ
# options.add_argument('--headless')
# driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
res = requests.get(url,headers=headers,timeout=3)
time.sleep(3)
soup = BeautifulSoup(res.text,"html.parser")
code_list = []
try:
for i in range(1,51):
select = "#newpresSchedule > div.m-block.newpresSearchResults > div > div.m-artcle > table > tbody > tr:nth-child("+str(i)+") > td:nth-child(2) > a"
aaa = str(soup.select(select)[0].string)
code_list.append(aaa)
driver.get(url)
time.sleep(2)
element = driver.find_element_by_xpath('//*[@id="newpresSchedule"]/div[3]/div/div[3]/div/ul/li[3]/a')
element.click()
time.sleep(2)
soup = BeautifulSoup(driver.page_source,"html.parser")
#51以上ある場合に以下が実行される。
for i in range(1,51):
select = "#newpresSchedule > div.m-block.newpresSearchResults > div > div.m-artcle > table > tbody > tr:nth-child("+str(i)+") > td:nth-child(2) > a"
aaa = str(soup.select(select)[0].string)
code_list.append(aaa)
#101以上ある場合に以下が実行される。
driver.get(url)
time.sleep(2)
element = driver.find_element_by_xpath('//*[@id="newpresSchedule"]/div[3]/div/div[3]/div/ul/li[4]/a')
element.click()
time.sleep(2)
soup = BeautifulSoup(driver.page_source,"html.parser")
for i in range(1,51):
select = "#newpresSchedule > div.m-block.newpresSearchResults > div > div.m-artcle > table > tbody > tr:nth-child("+str(i)+") > td:nth-child(2) > a"
aaa = str(soup.select(select)[0].string)
code_list.append(aaa)
#151以上ある場合に以下が実行される。
driver.get(url)
time.sleep(2)
element = driver.find_element_by_xpath('//*[@id="newpresSchedule"]/div[3]/div/div[3]/div/ul/li[5]/a')
element.click()
time.sleep(2)
soup = BeautifulSoup(driver.page_source,"html.parser")
for i in range(1,51):
select = "#newpresSchedule > div.m-block.newpresSearchResults > div > div.m-artcle > table > tbody > tr:nth-child("+str(i)+") > td:nth-child(2) > a"
aaa = str(soup.select(select)[0].string)
code_list.append(aaa)
#201以上ある場合に以下が実行される。
driver.get(url)
time.sleep(2)
element = driver.find_element_by_xpath('//*[@id="newpresSchedule"]/div[3]/div/div[3]/div/ul/li[6]/a')
element.click()
time.sleep(2)
soup = BeautifulSoup(driver.page_source,"html.parser")
for i in range(1,51):
select = "#newpresSchedule > div.m-block.newpresSearchResults > div > div.m-artcle > table > tbody > tr:nth-child("+str(i)+") > td:nth-child(2) > a"
aaa = str(soup.select(select)[0].string)
code_listappend(aaa)
except:
print("例外")
print(code_list)
print(len(code_list))