반응형

 

설치

pip install selenium

 

1.chromedriver를 사용시 

https://chromedriver.storage.googleapis.com/index.html?path=97.0.4692.71/ 

 

https://chromedriver.storage.googleapis.com/index.html?path=97.0.4692.71/

 

chromedriver.storage.googleapis.com

에서 운영체제에 맞는 드라이버 설치후 프로젝트 폴더에 넣어줍니다.

 

2.chromedriver를 사용하지 않을시

pip install webdriver-manager

 

 

로그인코드

import time
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 webdriver_manager.chrome import ChromeDriverManager as CM

TIMEOUT = 5


def login():

    #패킷수정(헤더수정)
    options = webdriver.ChromeOptions()
    #창없이 실행
    # options.add_argument("--headless")
    # #보안 기능인 샌드박스를 비활성하
    # options.add_argument('--no-sandbox')
    # #로그표시간소화? headless모드시 로그 줄여줌
    # options.add_argument("--log-level=3")
    #헤더수정 - 모바일에서 하는것처럼 해줘야되는듯
    mobile_emulation = {
        "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/90.0.1025.166 Mobile Safari/535.19"}
    options.add_experimental_option("mobileEmulation", mobile_emulation)
    #webdirver-manager 사용
    #driver = webdriver.Chrome(executable_path=CM().install(), options=options)
    driver = webdriver.Chrome('./chromedriver.exe', options=options)
    driver.set_window_size(600, 1000)
    #인스타 로그인창열기
    driver.get('https://www.instagram.com/accounts/login/')
    time.sleep(2)

    print("[Info] - Logging in...")

    # 명시적대기 TIMEOUT초동안 찾을때까지 WebDriverWait
    username = WebDriverWait(driver, TIMEOUT).until(
        EC.presence_of_element_located((
            By.XPATH, '//*[@id="loginForm"]/div[1]/div[3]/div/label/input')))
    #입력
    username.send_keys('test@gmail.com')


    password = WebDriverWait(driver, TIMEOUT).until(
        # #원하는 요소 XPath로 찾기
        # EC.presence_of_element_located((
        #      By.XPATH, '//*[@id="loginForm"]/div[1]/div[4]/div/label/input'))
        #name으로 찾기
        EC.presence_of_element_located((
            By.NAME,'password'))
        #driver.find_element_by_name('password')
    )


    password.send_keys('test')


    log_in = WebDriverWait(driver, TIMEOUT).until(
        EC.presence_of_element_located((
            By.XPATH, '//*[@id="loginForm"]/div[1]/div[6]/button')))

    time.sleep(1)

    log_in.click()

    time.sleep(5)



if __name__ == '__main__':
    login()

 

이후 필요한 봇의 기능들은 위방식대로 요소를 찾아서 입력 혹은 크롤링해온뒤 원하는 작업을 실행시키면 됩니다.

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기