Instagramの自動いいね返しについて
Q&A
解決したいこと
インスタグラムのいいねしてれた人に
自動でいいねを返すツールについてです。
ログインまではできたのですがその先の処理がどこかで間違っているようで
実行結果が言い値返しをした人は0人となっています。
selenium 4.1.0 です。
発生している問題・エラー
Please use find_element() instead
loginButton = driver.find_element_by_css_selector("button[type=submit]")
いいね返しを開始します
今回は「49人」にいいね返しをおこなう予定です
開始時刻:2023年01月19日 00:12:07
======================
C:\Users\user\Documents\Mypython\instagram.py:73: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
notification_list = driver.find_elements_by_class_name("_aaiy".replace(" ", "."))
===================
いいね返しが完了しました!
いいねを押した数:0回
完了時刻:2023年01月19日 00:12:25
### 該当するソースコード
# アクティビティフィードを開く
driver.get('https://www.instagram.com/accounts/activity/')
time.sleep(random.randint(5,8))
# いいねの人数・回数をリセット
like_total_people_num = 0
like_total_num = 0
# 開始時間を取得
dt_start_now = datetime.datetime.now()
print('いいね返しを開始します')
print(f'今回は「{like_return_number_of_people}人」にいいね返しをおこなう予定です')
print(f"開始時刻:{dt_start_now.strftime('%Y年%m月%d日 %H:%M:%S')}")
print('======================')
# 通知一覧を取得
notification_list = driver.find_elements_by_class_name("_aaiy".replace(" ", "."))
for notification_i, notification in enumerate(notification_list):
# 設定エリアの回数を超えたらループ処理を終了させる
if like_total_people_num >= like_return_number_of_people:
print("指定回数に到達しました")
break
# 通知の文章を取得
notification_text = notification.find_element_by_tag_name("span")
# ユーザー名を取得
user_name_text = notification.find_element_by_tag_name("a")
print(f"▼{notification_i+1}人目:{user_name_text.text}さん")
# いいね以外の場合、スキップして次の人へ
if not 'があなたの写真に「いいね!」しました' in notification_text.text:
print('└いいね以外なのでスキップします')
time.sleep(random.randint(2,3))
continue
# 1つ前にチェックしたユーザーと同じだった場合はスキップ
if notification_i > 0 and user_name_text.text == previous_user_name:
print("└1つ前のユーザーと同じなのでスキップします")
time.sleep(random.randint(2,3))
continue
print("└いいね返しします")
like_total_people_num += 1
previous_user_name = user_name_text.text
# ユーザー名をクリックして新しいタブで開く
# 実行OSがwindowsの場合の処理
if os.name == "nt":
ActionChains(driver).move_to_element(user_name_text).key_down(Keys.CONTROL).click().key_up(Keys.CONTROL).perform()
# 実行OSがMacの場合の処理
else:
ActionChains(driver).move_to_element(user_name_text).key_down(Keys.COMMAND).click().key_up(Keys.COMMAND).perform()
# 現在開いてるタブを取得し、新しく開いたタブに切り替える
handle_array = driver.window_handles
driver.switch_to.window(handle_array[1])
time.sleep(random.randint(7,8))
try:
# 1つ目の投稿をクリックさせる
driver.find_element_by_class_name('_aagu').click()
time.sleep(random.randint(3,5))
# いいね!ボタンの位置と子要素を取得し、いいね済かどうかのテキストを取得する
like_check_target_div = driver.find_element_by_class_name("_aamw")
like_check_target_element = like_check_target_div.find_element_by_class_name('_ab6-')
like_check_text = like_check_target_element.get_attribute('aria-label')
if like_check_text == "いいね!":
# いいねボタンを押す
like_check_target_div.click()
print(f'└いいねしました')
like_total_num += 1
time.sleep(random.randint(3,5))
else:
print(f'└いいね済みなのでスキップします')
time.sleep(random.randint(2,3))
except WebDriverException:
print('└何らかのエラーによりいいねできませんでした')
finally:
driver.close()
time.sleep(random.randint(3,5))
# 現在開いてるタブを取得し、最初のタブに切り替える
handle_array = driver.window_handles
driver.switch_to.window(handle_array[0])
# ブラウザを閉じる
driver.quit()
# 終了時間の取得・出力
dt_finish_now = datetime.datetime.now()
print('===================')
print('いいね返しが完了しました!')
print(f"いいねを押した数:{like_total_num}回")
print(f"完了時刻:{dt_finish_now.strftime('%Y年%m月%d日 %H:%M:%S')}")
自分で試したこと
昨夜は朝までひたすら出てきたエラーについてググって、今日も夕方と夜中時間を使ってほかのコードを試したりした結果です。。。すみません、初心者なのでどうしていいかわからず申し訳ございません。
お忙しい中恐縮ですが宜しくお願い致します。