스케쥴링 내용은 각각 한글 태그, 영문태그인데, 한국인이 활동하는 06~24시는 한글태그, 그외에 영어를 사용하는 외국인 활동시간을 00~06시 타겟으로 진행하였다. 횟수는 파이썬 코드에 하드코딩되어 있다. 매번 내가 입력할수도 있지만, 그렇다면 자동화가 아니기에 미리 계산해서 넣었다.
동시에 작업하면 더 좋지 않은가, 시간 간격을 20초 이내로 하면 더 빠르지 않겠냐는 의문이 생길순 있겠지만,
인스타그램에 불법으로 걸리지 않게끔 하기 위해서 조정하였다.
어제 20~30개 좋아요 수준이었던 내 최신 게시글이 왠지 모르게 중간에 파이썬이 멈추기는 하였지만,
C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe C:/02.lab/20210714_insta_py/01_like_bot.py
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 947, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1416, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 지정된 파일을 찾을 수 없습니다
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\02.lab\20210714_insta_py\01_like_bot.py", line 2, in <module>
driver = webdriver.Chrome('C:\02.lab\20210714_insta_py')
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: '.lab10714_insta_py' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Process finished with exit code 1
# -*- coding: utf-8 -*-
from selenium import webdriver
import inspect, os, platform, time
def bot():
#필요한 변수 정의
insta_id = input('인스타그램 아이디 : ')
insta_pw = input('인스타그램 패스워드 : ')
insta_tag = input('작업할 해시태그 : ')
insta_cnt = int(input('작업횟수(숫자만) : '))
#크롬드라이버 로딩
options = webdriver.ChromeOptions()
options.add_argument('--disable-gpu')
options.add_argument('user-agent=Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
current_folder = os.path.realpath( os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0]))
if platform.system() == 'Windows':
driver_path = os.path.join(current_folder, 'chromedriver.exe')
else:
driver_path = os.path.join(current_folder, 'chromedriver')
driver = webdriver.Chrome(driver_path, options=options)
driver.implicitly_wait(10)
### 인스타그램 자동 좋아요 작업 ###
#
# 1. 인스타그램 로그인 페이지로 이동
driver.get('https://www.instagram.com/?hl=ko')
print('로그인중....')
time.sleep(3)
# 2. 아이디 입력창을 찾아서 위에서 입력받은 아이디(insta_id)값 입력
id_input = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label')
id_input.click() #입력창 클릭
id_input.send_keys(insta_id)
# 아이디 입력 # 2-1. 패스워드 입력창을 찾아서 위에서 입력받은 패스워드(insta_pw)값 입력
pw_input = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label')
pw_input.click()
pw_input.send_keys(insta_pw)
# 3. 로그인 버튼 클릭
login_btn = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]/button')
login_btn.click() # 잠시 대기 time.sleep(3)
# 4. 작업할 해시태그 검색 결과 페이지로 이동
driver.get('https://www.instagram.com/explore/tags/{}/'.format(insta_tag))
time.sleep(2)
# 5. 인기게시물 첫번째 피드 선택
first_feed = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]')
first_feed.click()
time.sleep(1)
# 6. 좋아요 작업 - 입력한 횟수만큼 반복 작업
for idx in range(insta_cnt):
div = driver.find_element_by_xpath('/html/body/div[5]/div[2]/div/article/div')
div = div.find_element_by_xpath('/html/body/div[5]/div[2]/div/article/div[3]')
like_btn = div.find_element_by_tag_name('button') #좋아요 버튼
like_btn.click() #좋아요 클릭
print('{}번째 피드 좋아요 작업 완료'.format(idx + 1))
# 너무 빠르게 작업을 할 경우 많은 양의 작업을 하게 되어 인스타그램측에서 계정 정지나 경고를 할 수 있으니
# 작업과 다음 작업 사이의 속도를 조절하기 위해 20초 이상을 설정해주세요.
time.sleep(23)
# 7. 좋아요 작업 - 다음 피드로 이동
if idx < insta_cnt:
next_feed = driver.find_element_by_link_text('다음')
next_feed.click()
print('모든 작업 완료')
driver.quit()
bot()