print("== 実行中のスクリプト: タイトル・タグ・カテゴリ・公開設定更新版 ==")
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import time
from datetime import datetime
from selenium.webdriver.chrome.options import Options
# --- 設定エリア ---
driver_path = r"C:oolschromedriverchromedriver.exe"
input_file = r"blog_articles_with_tags.xlsx"
login_needed = True
your_blog_url = "https://yozda.exblog.jp/"
your_login_id = "yozda"
your_login_pw = "************************"
blog_base_edit_url = "https://userconf.exblog.jp/editor/?serial="
# --- ここまで設定エリア ---
def now():
return datetime.now().strftime("[%H:%M:%S]")
start_time = time.time()
print(f"{now()} 開始しました")
# Chromeオプション設定
chrome_options = Options()
chrome_options.add_argument("--log-level=3")
service = Service(driver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
# ログイン
if login_needed:
driver.get("https://ssl2.excite.co.jp/idc/login/")
wait = WebDriverWait(driver, 15)
email_input = wait.until(EC.presence_of_element_located((By.NAME, "loginid")))
email_input.send_keys(your_login_id)
password_input = driver.find_element(By.NAME, "password")
password_input.send_keys(your_login_pw)
login_button = driver.find_element(By.CLASS_NAME, "btn_login")
login_button.click()
time.sleep(3)
# ブログを開く
driver.get(f"{your_blog_url}")
time.sleep(2)
# Excel読み込み
df = pd.read_excel(input_file)
df_on = df[df["更新"].str.lower() == "on"]
total_articles = len(df_on)
print(f"{now()} 更新対象記事数: {total_articles}件")
for idx_in_loop, (idx, row) in enumerate(df_on.iterrows(), start=1):
print(f"{now()} ★ {idx_in_loop}件目 / {total_articles}件中 ★")
url = row["リンク"]
serial_id = url.split("/")[-2]
edit_url = blog_base_edit_url + serial_id
print(f"{now()} 編集ページへ移動: {edit_url}")
driver.get(edit_url)
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "entrySubject")))
time.sleep(0.5)
except:
print(f"{now()} ページ読み込みエラー(スキップ)")
continue
# タイトル更新
try:
title_input = driver.find_element(By.ID, "entrySubject")
title_input.clear()
title_input.send_keys(str(row["タイトル"]))
except:
print(f"{now()} タイトル更新エラー")
# タグ更新
try:
for i in range(1, 4):
tag_col = f"タグ{i}"
tag_value = str(row[tag_col]) if pd.notna(row[tag_col]) else ""
tag_input = driver.find_element(By.ID, f"_tag{i}")
tag_input.clear()
if tag_value:
tag_input.send_keys(tag_value)
except:
print(f"{now()} タグ更新エラー")
# カテゴリ更新
try:
category_value = str(row["カテゴリ"]).strip()
select_elem = Select(driver.find_element(By.NAME, "cgiid"))
matched = False
for option in select_elem.options:
if option.text.strip() == category_value:
select_elem.select_by_visible_text(option.text.strip())
matched = True
break
if not matched:
print(f"{now()} カテゴリ未一致(スキップ): {category_value}")
except:
print(f"{now()} カテゴリ更新エラー")
# 公開設定更新(公開/非公開)
try:
pub_state = str(row["公開状態"]).strip()
if pub_state == "公開":
pub_input = driver.find_element(By.CSS_SELECTOR, "input[name='openflag'][value='1']")
elif pub_state == "非公開":
pub_input = driver.find_element(By.CSS_SELECTOR, "input[name='openflag'][value='0']")
else:
pub_input = None # 対応しない文字列の場合はスキップ
if pub_input and not pub_input.is_selected():
pub_input.click()
except Exception as e:
print(f"{time.strftime('%H:%M:%S')} 公開設定エラー: {e}")
# 保存
try:
save_button = driver.find_element(By.ID, "edit")
save_button.click()
print(f"{now()} 保存完了: {row['タイトル']}")
except:
print(f"{now()} 保存ボタン押下エラー")
time.sleep(2)
elapsed = time.time() - start_time
print(f"{now()} すべて完了しました!所要時間: {elapsed:.1f}秒")
driver.quit()