import requests
from bs4 import BeautifulSoup
# URL
url = "https://www.jpx.co.jp/listing/others/ex-rights/index.html"
# URLからHTMLを取得
response = requests.get(url)
html = response.text
# BeautifulSoupオブジェクトを作成
soup = BeautifulSoup(html, 'html.parser')
# 最初のxlsファイルのリンクを見つける
link = soup.find('a', href=lambda href: href and href.endswith('.xls'))
# ベースURL
base_url = 'https://www.jpx.co.jp'
# フルURLを作成
full_url = base_url + link['href']
# ファイルをダウンロード
response = requests.get(full_url)
with open('jpx_配当落権利落等情報.xls', 'wb') as f:
f.write(response.content)