【Python】csvファイルの値をWeb画面で入力する
csvファイルの値をWeb画面で入力します。
インポート機能がないWebシステムや、マスタへの連続登録処理にも転用できます。
処理内容
①csvファイルのチェック
②Web画面を開く
③csvファイルの値をWeb画面で入力
④Webの計算処理を実行
⑤処理結果をメッセージボックスで表示
②Web画面を開く
③csvファイルの値をWeb画面で入力
④Webの計算処理を実行
⑤処理結果をメッセージボックスで表示
ke!sanの各種計算機能
今回はke!san様(https://keisan.casio.jp/exec/system/1183427246)のエネルギー必要量計算画面を使わせていただきます。
値の入力→計算の実行→結果の出力をPythonで処理します。

csvファイルの準備
次のようなcsvファイルを準備しておきます。
- ファイル名はinput.csv
- 1行目はヘッダ
- 年齢、性別、身体活動レベル、目標体重の値を入力しておく

PythonによるWeb入力ツールの作成
環境に合わせて以下を修正してください。
- ChromeDriverのパス
- ブラウザの動作を確認したい場合は41行目のヘッドレスモードのオプションをコメントアウト
# ---------------------------------------- # モジュールのインポート # ---------------------------------------- from selenium import webdriver import os import csv import tkinter as tk from tkinter import messagebox # メッセージボックス用の設定 root = tk.Tk() root.withdraw() # ---------------------------------------- # csvファイルの読み込み # ---------------------------------------- csv_file = 'input.csv' if os.path.exists(csv_file): with open(csv_file, 'r', encoding='utf8') as f: reader = csv.reader(f) line = [row for row in reader] input_row = len(line) print('処理対象件数: ' + str(input_row - 1)) if input_row < 2: messagebox.showwarning('件数チェックエラー', '処理対象データがないため処理を終了します。') exit() else: messagebox.showerror('ファイルチェックエラー', '『' + csv_file + '』が存在しないため処理を終了します。') exit() # ---------------------------------------- # ChromeDriverの設定 # ---------------------------------------- cd_path = 'C:\\ChromeDriver\\chromedriver.exe' chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_experimental_option('excludeSwitches', ['enable-automation', 'enable-logging']) # ---------------------------------------- # 処理開始 # ---------------------------------------- print('>>>処理開始') driver = webdriver.Chrome(cd_path, options=chrome_options) driver.maximize_window() driver.implicitly_wait(10) driver.get('https://keisan.casio.jp/exec/system/1183427246/') # ---------------------------------------- # 入力に使用する画面要素の設定 # ---------------------------------------- age_xpath = '//*[@id="var_age"]' sx0_xpath = '//*[@id="inparea"]/tbody/tr[2]/td[2]/ul/ol/li[5]/label[1]' sx1_xpath = '//*[@id="inparea"]/tbody/tr[2]/td[2]/ul/ol/li[5]/label[2]' alv1_xpath = '//*[@id="inparea"]/tbody/tr[3]/td[2]/ul/ol/li[1]/label[1]' alv2_xpath = '//*[@id="inparea"]/tbody/tr[3]/td[2]/ul/ol/li[1]/label[2]' alv3_xpath = '//*[@id="inparea"]/tbody/tr[3]/td[2]/ul/ol/li[1]/label[3]' kg_xpath = '//*[@id="var_kg"]' ans0_xpath = '//*[@id="ans0"]' execute_xpath = '//*[@id="executebtn"]' clear_xpath = '//*[@id="clearbtn"]' # ---------------------------------------- # 情報の入力と計算の実行 # ---------------------------------------- for i in range(1, input_row): age = line[i][0] sex = line[i][1] act_level = line[i][2] weight = line[i][3] driver.find_element_by_xpath(age_xpath).send_keys(age) if sex == '男': driver.find_element_by_xpath(sx0_xpath).click() elif sex == '女': driver.find_element_by_xpath(sx1_xpath).click() if act_level == '低い': driver.find_element_by_xpath(alv1_xpath).click() elif act_level == '高い': driver.find_element_by_xpath(alv3_xpath).click() else: driver.find_element_by_xpath(alv2_xpath).click() driver.find_element_by_xpath(kg_xpath).send_keys(weight) driver.find_element_by_xpath(execute_xpath).click() # 計算結果の表示 energy = driver.find_element_by_xpath(ans0_xpath).text message = str(i) + '/' + str(input_row - 1) + '件目' + ''' \n1日に必要なエネルギー量は ''' + energy + ' Kcalです' messagebox.showinfo('計算結果', message) driver.find_element_by_xpath(clear_xpath).click() # ---------------------------------------- # 処理終了 # ---------------------------------------- print('<<<処理終了') messagebox.showinfo('処理終了', '処理が終了しました') driver.quit()
実行結果
実行すると、処理件数と計算結果をメッセージボックスで表示します。

参考書
この本は所々に書き込む余白があるので「自分の本」が育ちます。
リンク
最近のコメント