반응형
좋아요 자동 동작 코드를 보기 전에 자동 로그인과 자동 검색을 아래 링크서 먼저 확인하고 보는것을 추천한다.
scribblinganything.tistory.com/20?category=943911
scribblinganything.tistory.com/25?category=943911
코드>>
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.common.keys import Keys
import time
class InstagramAutomation():
def log_in(self):
options = webdriver.ChromeOptions()
# options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64)/AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36")
self.driver = webdriver.Chrome(options=options)
self.driver.get("https://www.instagram.com/")
# time.sleep(3)
try:
username_box_check = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '//*[@id="loginForm"]/div/div[1]/div/label/input')))
print(username_box_check)
except:
print("error")
username_box = self.driver.find_elements_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')[0]
username_box.send_keys("your ID")
password_box = self.driver.find_elements_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')[0]
password_box.send_keys("your PW")
login_button = self.driver.find_elements_by_xpath('//*[@id="loginForm"]/div/div[3]/button')[0]
login_button.click()
def searchHashtag(self, hashtag):
try:
search_input = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input')))
print(search_input)
search_input.send_keys("#"+hashtag)
first_index = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[4]/div/a[1]')))
first_index.click()
except:
print("error")
def like_pic(self, number):
if number > 0:
first_pic = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a')))
first_pic.click()
like_action = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button')))
# /html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button/div/span/svg
# aria-label
like_path = self.driver.find_element_by_xpath('/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button/div/span/*[name()="svg"]')
like_text = like_path.get_attribute('aria-label')
print(like_text)
if like_text == "좋아요":
print("좋아요 안되어 있으므로 클릭하기")
like_action.click()
else:
print("좋아요 되어 있음")
if number > 1:
next_btn = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[5]/div[1]/div/div/a')))
next_btn.click()
while number > 1:
like_action = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button')))
like_path = self.driver.find_element_by_xpath('/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button/div/span/*[name()="svg"]')
like_text = like_path.get_attribute('aria-label')
print(like_text)
if like_text == "좋아요":
print("좋아요 안되어 있으므로 클릭하기")
like_action.click()
else:
print("좋아요 되어 있음")
next_btn = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[5]/div[1]/div/div/a[2]')))
next_btn.click()
number = number - 1
oop_instagram = InstagramAutomation()
oop_instagram.log_in()
oop_instagram.searchHashtag("BTS")
oop_instagram.like_pic(10)
time.sleep(1000)
결과>>
주석>>
def like_pic(self, number):
if number > 0:
first_pic = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a')))
first_pic.click()
like_action = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button')))
# /html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button/div/span/svg
# aria-label
like_path = self.driver.find_element_by_xpath('/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button/div/span/*[name()="svg"]')
like_text = like_path.get_attribute('aria-label')
print(like_text)
if like_text == "좋아요":
print("좋아요 안되어 있으므로 클릭하기")
like_action.click()
else:
print("좋아요 되어 있음")
if number > 1:
next_btn = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[5]/div[1]/div/div/a')))
next_btn.click()
while number > 1:
like_action = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button')))
like_path = self.driver.find_element_by_xpath('/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button/div/span/*[name()="svg"]')
like_text = like_path.get_attribute('aria-label')
print(like_text)
if like_text == "좋아요":
print("좋아요 안되어 있으므로 클릭하기")
like_action.click()
else:
print("좋아요 되어 있음")
next_btn = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located\
((By.XPATH, '/html/body/div[5]/div[1]/div/div/a[2]')))
next_btn.click()
number = number - 1
위 코드가 좋아요를 동작하게 하는 코드이다.
동작은 selenium 모듈을 사용하였다.
동작은 인자에 입력된 number 숫자만큼 사진/영상의 "좋아요" 를 클릭한다.
주의 해야할 부분 중에 좋아요가 이미 클릭되어 있다면 다시 클릭하면 좋아요가 사라지므로 좋아요가 클린된 상태인지 아닌지를 확인해야 한다.
내가 사용한 부분은 element에서 svg 부분에 aria-label에 적힌 부분이 좋아요가 클릭된 상태이면 "좋아요 취소" 였고 좋아요가 클릭 안된 상태이면 "좋아요"만이 attribute에 있었다.
svg는 해당 xpath를 가져와서는 읽을 수 없었다. action부분이라 /*[name()="svg"] 으로 바꿔서 xpath에 넣어야 해당 부분을 찾을 수 있었다. 그리고 attribute를 가져와서 비교 하였다.
그리고 number 만큼 좋아요를 클릭하게 while 문으로 동작 시켰다.
반응형
'파이썬(Python) > 인스타그램' 카테고리의 다른 글
파이썬으로 인스타그램 자동 댓글 남기기(python instagram comment automation) (0) | 2021.01.25 |
---|---|
인스타그램 자동검색(해시태그) (0) | 2020.12.03 |
인스타그램 자동 로그인 (Instagram Log in) (4) | 2020.12.01 |