くっそー。入口間違えたわ。普通に考えて日記タグ700記事とかに付けたらガイドライン違反だもんなぁ・・・。
とりあえず、最新20記事の指定したカテゴリをタグに変えるプログラムです。
ビューティフルスープがうまく使えなくてめちゃ時間かかった(-_-;)
#よくわかってない from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from bs4 import BeautifulSoup #タグをセットするカテゴリ setTags=['C#','Excel','JavaScript','jQuery','Outlook','php','PowerPoint','python3','vba','Word','FC東京'] #chrome開くよ with webdriver.Chrome() as driver: #よくわかってない wait = WebDriverWait(driver, 10) #記事の管理画面のURL driver.get("https://blog.hatena.ne.jp/elve/elve.hatenadiary.jp/entries") #ログイン 画面 driver.find_element_by_xpath("//*[@id='login-name']").send_keys("はてなID") driver.find_element_by_xpath("//*[@id='container']/div[1]/form[1]/div/div[2]/div/input").send_keys("パスワード") driver.find_element_by_xpath("//*[@id='option']/input[2]").send_keys(Keys.RETURN) WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)#読み込み待ち #記事の管理 画面 html = driver.page_source.encode('utf-8') #解析用 soup = BeautifulSoup(html, 'lxml') # Store the ID of the original window 現在画面のハンドル保存 original_window = driver.current_window_handle #テーブルの行 i=1 #tr-hoverの行数分ループ(20) for row in soup.find_all("tr",class_="tr-hover"): items=""#カテゴリ item2=""#設定済みタグ #カテゴリ取得 items = row.find_all('td',class_="td-blog-category") #編集ボタンを押す driver.find_element_by_xpath("//*[@id='admin-main-wrapper-inner']/div[2]/section/div[6]/table/tbody/tr[" + str(i) + "]/td[2]/div/div/a").send_keys(Keys.RETURN) WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)#読み込み待ち #編集->編集を押す driver.find_element_by_xpath("//*[@id='admin-main-wrapper-inner']/div[2]/section/div[6]/table/tbody/tr[" + str(i) + "]/td[2]/div/div/div/ul/li[1]/a").send_keys(Keys.CONTROL,Keys.RETURN) WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)#読み込み待ち #個別記事の編集画面(別タブ) driver.switch_to.window(driver.window_handles[-1]) #編集画面読み込み html2 = driver.page_source.encode('utf-8') soup2 = BeautifulSoup(html2, 'lxml') item2 = soup2.find_all("ul", class_="editor-tag-editor_tag-list") #tagリスト if item2[0].text == "": #タグが付いてない #タグをつける driver.find_element_by_xpath("//*[@id='tag-editor-root']/div/button").send_keys(Keys.RETURN) WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)#読み込み待ち try: #初回のみの画面が出ちゃう時 driver.find_element_by_xpath("/html/body/div[10]/div/div/div/button").send_keys(Keys.RETURN) except: #しょうがないからprint print("OK:",i) WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)#読み込み待ち flg ="false" for item in items: for tag in setTags: #指定されたタグと同じカテゴリならタグ設定 if item.text.replace("\n","")==tag: driver.find_element_by_xpath("/html/body/div[11]/div/div/div[1]/div[2]/span/input").send_keys(item.text) driver.find_element_by_xpath("/html/body/div[11]/div/div/div[1]/div[2]/span/input").send_keys(Keys.RETURN) flg="true" if flg=="true": #タグ設定してたら更新 driver.find_element_by_xpath("/html/body/div[11]/div/div/div[2]/button").send_keys(Keys.RETURN) WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)#読み込み待ち driver.find_element_by_xpath("//*[@id='submit-button']").send_keys(Keys.RETURN) WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)#読み込み待ち #Close the tab or window タブを閉じる driver.close() #Switch back to the old tab or window 保存してた元のハンドラ driver.switch_to.window(original_window) WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)#読み込み待ち #次の行へ i+=1
次のページに行こうとしたらページじゃなくて下に読み込むんだもーん(-_-;)うぅぅう
たぶん記事の管理画面から行こうとしたのが間違いで
https://snack.elve.club/archive/category/カテゴリー
から記事を開いてタグ付だな。
明日やる
参考ページ
ほかにも基本的なことたくさんあちこち参考にしました(-_-;)
Seleniumブラウザー自動化プロジェクト :: Seleniumドキュメント
SeleniumとBeautifulsoupを利用してPythonでスクレイピングする方法を現役エンジニアが解説【初心者向け】 | TechAcademyマガジン
ブラウザー操作 :: Seleniumドキュメント
Python3の配列でよく使う小技のまとめ - suzu6
【Python】BeautifulSoupのfindとfind_allの使い方【スクレイピング】 - narupoのブログ
Python3メモ - BeautifulSoup4のあれこれ - Szarny.io
[Python入門]Beautiful Soup 4によるスクレイピングの基礎 (2/2):Python入門 - @IT
Pythonのwhile文によるループ処理(無限ループなど) | note.nkmk.me
Pythonの例外処理(try, except, else, finally) | note.nkmk.me
https://www.isc.meiji.ac.jp/~mizutani/python/intro3_python.html
pythonで自動入力、スクレイピングをしてみる - Qiita
Seleniumで待機処理するお話 - Qiita