python seleniumインストール手順

まずchromedriveをインストール

GoogleChromeのバージョンの確認する。ヘルプ>GoogleChromeについて
バージョンを付けてpip install

pip install chromedriver-binary==103.0.5060.114

エラーになったら、そのメッセージ内にある一番近いバージョンを入力する

pip install chromedriver-binary==見つけたバージョン

続いてseleniumインストール

pip install selenium

ネタ元

miyashinblog.com

MFC CInternetSessionでHTTPS接続に対応

// str_url = https://なURL
// api叩くとjson返すのでpicojson使ってる
picojson::object func_api_req(const CString& str_url) {

    // Using MFC WinINet classes
    CInternetSession session(_T("My Session"), 1, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE);
    CHttpConnection* pServer = NULL;
    CHttpFile* pFile = NULL;

    CString strServerName;
    INTERNET_PORT nPort;
    CString strObject;
    DWORD dwServiceType;

    // Breaking the URL into its components by using CrackUrl()
    if (!AfxParseURL(str_url, dwServiceType, strServerName, strObject, nPort) ||
        dwServiceType != INTERNET_SERVICE_HTTP)
    {
        // URL is malformed. Error handling here.
        throw std::runtime_error("Malformed URL");
    }

    // Opening a connection to the HTTP server
    try {
        pServer = session.GetHttpConnection(strServerName, nPort);
        // Sending a request to the server
        pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD);
        pFile->SendRequest();
    }
    catch (CInternetException* pEx) {
        // エラーメッセージの表示
        TCHAR sz[1024];
        pEx->GetErrorMessage(sz, 1024);
        std::cerr << "Caught a CInternetException: " << sz << std::endl;

        // 例外オブジェクトの削除
        pEx->Delete();

        if (pServer != NULL) delete pServer;
        throw std::runtime_error("HTTP request failed");
    }
    catch (...) {
        if (pServer != NULL) delete pServer;
        throw;
    }

    // Insert the response processing code here
    picojson::object obj;

    return obj;
}

このコードでは CInternetSession オブジェクトの作成時に INTERNET_FLAG_SECURE フラグを使用して、HTTPS接続を有効にしています。

同様に OpenRequest() 関数も INTERNET_FLAG_SECURE フラグを使用してHTTPS接続を有効にしています。

ただし、上記のコードはHTTPS接続の基本的な設定のみを含んでおり、詳細な証明書の検証やエラーハンドリングは実装されていません。
そのため、本番環境での使用には、適切なエラーハンドリングと証明書の検証を追加することを強く推奨します。

MFCでファイル名渡すと関連付けされた実行ファイルを起動する

ShellExecuteEx()使います

// 実行ファイルのパスと名前
CString programPath = "hogehoge.txt";

// 実行フォルダのパス
CString workingDirectory = _T("C:\\Path\\To\\Working\\Directory");

// SHELLEXECUTEINFO構造体を初期化する
SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };
sei.fMask = SEE_MASK_NOCLOSEPROCESS; // プロセスハンドルを取得するためのフラグ
sei.lpVerb = _T("open");             // 操作("open"または"print"など)
sei.lpFile = programPath;               // ファイルのパス
sei.lpDirectory = workingDirectory;  // 実行フォルダのパス
sei.nShow = SW_SHOW;                 // 表示コマンド

// ShellExecuteEx関数を使用して関連付けられたアプリケーションを起動する
if (::ShellExecuteEx(&sei))
{
	// プロセスが終了するまで待つ
	::WaitForSingleObject(sei.hProcess, INFINITE);

	// プロセスハンドルを閉じる
	::CloseHandle(sei.hProcess);
}
else
{
	// エラーメッセージを表示する
	::AfxMessageBox(_T("コマンド実行に失敗しました。"));
}

python xlsから指定列のデータを読み取ってcsv出力

import pandas as pd

# xlsファイルを開く(5行目からデータを読み込む)
df = pd.read_excel('jpx_配当落権利落等情報.xls', skiprows=4)

# E列とC列のデータを取得(Pythonのインデックスは0から始まるため、E列は4、C列は2となります)
df = df.iloc[:, [4, 2]]

# ヘッダを設定
df.columns = ['コード', '権利落ち日']

# コード列の内容を文字列に変換し、先頭4文字だけにする
df['コード'] = df['コード'].astype(str).str[:4]

# データをcsvファイルに出力
df.to_csv('jpx_配当落権利落等情報.csv', index=False,encoding='shift-jis')

python 最初に見つかったxlsへのリンクを見つけてダウンロード

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)