2
5

More than 1 year has passed since last update.

Instagramのいいねを自動化したい

Last updated at Posted at 2023-02-11

Instagramでフォロワーを増やす必要があり、Instagramのいいねを自動化しました。
2023年2月12日に動作確認をしました。

注意: Instagram公式には"自動いいね"は推奨されていないため、ご利用は自己判断でお願いします。

実行環境

OS: macOS Ventura 13.1
Python: 3.10.10
Chrome: 109.0.5414.119

instagram_automate_like.py
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.action_chains import ActionChains
import time
import datetime
import bs4
import random
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager


def get_time_now():
    datetime_now = datetime.datetime.now()
    return datetime_now.strftime('%m/%d %H:%M') + ' '


# login information
your_username = 'enter_your_username'
your_password = 'enter_your_password'

# Write in tags that you search.
search_tags = ['bluesky', 'craftbeer']
target_tag = random.choice(search_tags)
print("you will like '" + target_tag + "' in this execution.")

# Set max iterations of Like
MAX_LIKES = 300

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
time.sleep(1)

driver.find_element(By.NAME, 'username').send_keys(your_username)
time.sleep(0.1)
driver.find_element(By.NAME, 'password').send_keys(your_password)
time.sleep(0.1)

driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]').click()
time.sleep(3)

# Search posts with target_tag
sns_url = 'https://www.instagram.com/explore/tags/'
driver.get(sns_url + target_tag)
time.sleep(3)

# Move to a latest post
target_post = driver.find_elements(By.CLASS_NAME, '_aagw')[10]
actions = ActionChains(driver)
actions.move_to_element(target_post)
actions.perform()
time.sleep(3)


def is_not_liked_before():
    html = driver.page_source.encode('utf-8')
    soup = bs4.BeautifulSoup(html, "lxml")
    like_button_status = soup.select('span._aamw')
    return '取り消す' not in str(like_button_status[0])


try:
    driver.find_elements(By.CLASS_NAME, '_aagw')[9].click()
    time.sleep(random.randint(3, 5))

    if is_not_liked_before():
        driver.find_elements(By.CLASS_NAME, '_aamw')[0].click()
        print(get_time_now() + 'Clicked Like')
        time.sleep(random.randint(3, 5))
    else:
        print(get_time_now() + 'Liked before')

except WebDriverException:
    print(get_time_now() + "WebDriverException Occurred")


for i in range(MAX_LIKES - 1):
    try:
        driver.find_element(By.CLASS_NAME, '_aaqg._aaqh').click()
        time.sleep(random.randint(3, 5))

    except WebDriverException:
        time.sleep(random.randint(3, 5))

    try:
        if is_not_liked_before():
            driver.find_elements(By.CLASS_NAME, '_aamw')[0].click()
            time.sleep(random.randint(3, 5))
        else:
            print(get_time_now() + 'Liked before')

    except WebDriverException:
        print(get_time_now() + "WebDriverException Occurred")


print(get_time_now() + 'Finished Like automation correctly')
driver.close()
driver.quit()

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5