概要
いいねを押すのにいちいちスクロールして、いいねを押して、戻って、の繰り返しが面倒だったので、自動でできないかやってみました。
必要なライブラリのインストール
まず、SeleniumとEdgeDriverを使用するために必要なライブラリをインストールします。
pip install selenium webdriver-manager
Step 1: EdgeDriverの設定
EdgeDriverをダウンロードし、必要なサービスを設定します。
from selenium import webdriver
from selenium.webdriver.edge.service import Service as EdgeService
from webdriver_manager.microsoft import EdgeChromiumDriverManager
service = EdgeService(executable_path=EdgeChromiumDriverManager().install())
driver = webdriver.Edge(service=service)
Step 2: ログインセッションの保存
ログインした状態を維持するために、クッキーを保存します。
import pickle
import time
driver.get("ログインページのURL")
input("ログイン後、Enterキーを押してください…")
with open("cookies.pkl", "wb") as file:
pickle.dump(driver.get_cookies(), file)
driver.quit()
Step 3: 自動いいねスクリプト
保存したクッキーを使用して、ログイン状態を復元し、いいねを自動化するスクリプトです。
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.common.exceptions import TimeoutException # ここを追加
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.edge.options import Options
from webdriver_manager.microsoft import EdgeChromiumDriverManager
import pickle
import time
# EdgeDriverのサービスを設定
service = EdgeService(executable_path=EdgeChromiumDriverManager().install())
# セキュリティ警告を無視するオプションを設定
options = Options()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
# Edgeを起動
driver = webdriver.Edge(service=service, options=options)
# クッキーを読み込み
driver.get("サイトのURL")
with open("cookies.pkl", "rb") as file:
cookies = pickle.load(file)
for cookie in cookies:
driver.add_cookie(cookie)
# ページを再読み込みしてセッションを復元
driver.refresh()
time.sleep(3)
try:
driver.get("サイトのURL")
time.sleep(3)
driver.get("アクティビティの検索ページのURL")
time.sleep(3)
iteration_count = 0
max_iterations = 100
while iteration_count < max_iterations:
iteration_count += 1
print(f"Iteration: {iteration_count}")
a_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "a.アクティビティリンクのCSSクラス"))
)
a_element.click()
time.sleep(3)
try:
span_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "span.いいねボタンのCSSクラス"))
)
if "active" in span_element.get_attribute("class"):
print("Button is already active. Cancelling the action.")
else:
span_element.click()
print("Button was not active. Clicking the button.")
except TimeoutException:
print("The 'like' button was not found within 10 seconds.")
time.sleep(3)
driver.get("アクティビティの検索ページのURL")
time.sleep(3)
except Exception as e:
print("An error occurred:", e)
finally:
driver.quit()
print("Browser closed")
エラーコードと対処方法
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
webdriver.Edgeを初期化する際に、executable_pathの代わりにServiceオブジェクトを使用します。
例:
from selenium.webdriver.edge.service import Service as EdgeService
service = EdgeService(executable_path='path/to/edgedriver')
driver = webdriver.Edge(service=service)
TimeoutException
要素が指定された時間内に見つからない場合に発生します。待機時間を増やすか、セレクタを再確認してください。
例:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
span_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "span.いいねボタンのCSSクラス")))
その他の例外
予期しないエラーが発生した場合は、エラーメッセージを確認し、適切な対処方法を検討してください。
例:
except Exception as e:
print("An error occurred:", e)
SSLエラー
ERROR:ssl_client_socket_impl.cc(1063)] handshake failedのようなSSLエラーが発生した場合、セキュリティ警告を無視するオプションを追加します。
例:
options = Options()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
これで、いいねを自動で押すスクリプトの完成です。エラーが発生した場合は、ログやエラーメッセージを確認し、対処方法を参考に修正してください。
以上になります。
何かの参考になれば幸いです。
技術書の購入コストを抑えてスキルアップするなら

ここまで読んでいただきありがとうございます。最後に宣伝をさせてください。
プログラミングの技術書や参考書は、1冊3,000円〜5,000円するものも多く、出費がかさみがちです。Kindle Unlimitedであれば、月額980円で500万冊以上の書籍が読み放題となります。
気になる言語の入門書から、アルゴリズム、基本設計の専門書まで、手元のスマホやPCですぐに参照可能です。現在は「30日間の無料体験」や、対象者限定の「3か月499円プラン」なども実施されています。まずはご自身のアカウントでどのようなオファーが表示されるか確認してみてください。
