본문 바로가기
코딩&프로그래밍

Selenium과 PyAutoGUI를 활용한 인스타그램 자동 로그인 방법

by dothink 2023. 9. 16.

웹 자동화를 위해 Python의 Selenium 라이브러리를 자주 사용하게 됩니다.

그러나 때때로 웹 서버에서 자동화 스크립트나 봇을 감지하여 차단하는 경우가 있습니다.

이 문제를 해결하기 위해 pyautogui와 pyperclip 라이브러리를 함께 사용하여

인스타그램에 자동 로그인하는 방법을 소개합니다.

 

1. 필요한 라이브러리 설치

pip install pyautogui pyperclip selenium

 

2. 라이브러리 호출

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pyautogui
import pyperclip

 

3. 로그인 자동화 스크립트 작성

3-1) 브라우저 옵션 설정: 브라우저가 종료되지 않도록 옵션 추가

 

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=chrome_options)

 

3-2) 인스타그램 접속

river.get("https://www.instagram.com")
time.sleep(5)

 

3-3) 아이디 및 패스워드 입력: 클립보드를 사용하여 입력값을 붙여 넣기

user = driver.find_element(By.XPATH, "//input[@name='username']")
user.click()
pyperclip.copy("#인스타그램ID")
pyautogui.hotkey("ctrl","v")
time.sleep(2)

passw = driver.find_element(By.XPATH, "//input[@name='password']")
passw.click()
pyperclip.copy("#인스타그램PW")
pyautogui.hotkey("ctrl", "v")
time.sleep(2)

 

3-4) 로그인 및 알림 설정 버튼 클릭

loginButton = driver.find_element(By.XPATH, "//button[@type='submit' and .//div[text()='로그인']]")
loginButton.click()
time.sleep(5)

login_save = driver.find_element(By.XPATH, "//button[text()='정보 저장']")
login_save.click()
time.sleep(1)
alert_setting = driver.find_element(By.XPATH, "//button[text()='나중에 하기']")
alert_setting.click()
time.sleep(1)

 


 

이러한 방법을 통해 웹 서버가 자동화 스크립트를 감지하는 것을 어렵게 만들 수 있습니다. 그러나 항상 웹 서비스의 이용 약관을 준수하고, 이러한 자동화 방법을 사용할 때 책임감을 가지고 행동해야 합니다.

댓글