【Selenium + Python】Wordpressの全部の記事で特定の文字を置換する

Selenium + Python を使って、Wordpressの全部の記事で特定の文字を置換するというのを自動化しました。

# coding:utf-8 import re

from selenium import webdriver

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.chrome.options import Options

from selenium.common.exceptions import TimeoutException

options = Options() options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary' options.add_argument('--headless') #driver = webdriver.Chrome(chrome_options=options) driver = webdriver.Chrome() driver.implicitly_wait(10) WAIT_TIME = 30

WORDPRESS_PATH = "https://【ドメイン】/wordpress" SERCH_KEYWORD = "【置換したい文字列】" SERCH_KEYWORD_AHTER = "【置換後の文字列】"

def login():

url = WORDPRESS_PATH + "/wp-login.php" driver.get(url)

WebDriverWait(driver, WAIT_TIME).until( EC.presence_of_element_located*1 )

driver.find_element_by_id("user_login").send_keys("【ユーザーID】") driver.find_element_by_id("user_pass").send_keys("【パスワード】") driver.find_element_by_id("wp-submit").click()

def main():

next()

def next():

url = WORDPRESS_PATH + "/wp-admin/edit.php?s=" + SERCH_KEYWORD # 投稿一覧 driver.get(url)

WebDriverWait(driver, WAIT_TIME).until( EC.presence_of_element_located*2 )

next_href = ""

try: WebDriverWait(driver, 0.1).until( EC.presence_of_element_located*3 ) next_btn = driver.find_element_by_class_name("next-page") next_href = next_btn.get_attribute("href") except TimeoutException as ex: print("TimeoutException")

# eles = driver.find_elements_by_class_name("next-page") # if len(eles) > 0: # next_btn = eles[0] # next_href = next_btn.get_attribute("href")

eles = driver.find_elements_by_class_name("row-title") list = [] for ele in eles: href = ele.get_attribute("href") list.append(href)

for href in list: update_contents(href)

if next_href != "": print("next_href = " + next_href) next()

def update_contents(url):

print("url = " + url)

driver.get(url)

WebDriverWait(driver, WAIT_TIME).until( EC.presence_of_element_located*4 )

textarea = driver.find_element_by_class_name("wp-editor-area") if textarea.text.find(SERCH_KEYWORD) > -1: txt = re.sub(SERCH_KEYWORD, SERCH_KEYWORD_AHTER, textarea.text) textarea.clear() textarea.send_keys(txt)

driver.find_element_by_id("publish").click()

WebDriverWait(driver, WAIT_TIME).until( EC.presence_of_element_located*5 )

login() main()

print("finish !!")

*1:By.ID, "user_login"

*2:By.ID, "post-search-input"

*3:By.CLASS_NAME, "next-page"

*4:By.CLASS_NAME, "wp-editor-area"

*5:By.ID, "message"