본문 바로가기
파이썬

39. 셀레니움-인스타그램

by 곽정우 2024. 5. 21.

1. 로그인

프로그램 실행:

import chromedriver_autoinstaller
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

chromedriver_autoinstaller.install()
driver = webdriver.Chrome()

url = 'https://www.instagram.com/'
driver.get(url)

id = '아이디'
pw = '비밀번호'

# 요소 로드를 기다림
wait = WebDriverWait(driver, 10)
input_id = wait.until(EC.visibility_of_element_located((By.NAME, 'username')))
input_pw = wait.until(EC.visibility_of_element_located((By.NAME, 'password')))

input_id.send_keys(id)
input_pw.send_keys(pw)

driver.find_element_by_css_selector('button[type="submit"]').click()

2. 해시태그 검색

프로그램 실행:

hashtag = '맛점'
url = f'https://www.instagram.com/explore/tags/{hashtag}/'
driver.get(url)

3. 스크롤 내리기

프로그램 실행:

import time

for _ in range(2):
    driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
    # time.sleep(3)

4. 원하는 사진 클릭하기

프로그램 실행:

driver.find_element('xpath','/html/body/div[2]/div/div/div[2]/div/div/div[1]/div[1]/div[2]/section/main/article/div/div[2]/div/div[2]/div[3]').click()

5. 좋아요 클릭하기

프로그램 실행:

driver.find_element('xpath','/html/body/div[8]/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[2]/div/div/div[2]/section[1]/span[1]/div/div').click()

6. 댓글 달기

프로그램 실행:

comment = '잘보고가요'

input_comment = driver.find_element('xpath','/html/body/div[8]/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[2]/div/div/div[2]/section[3]/div/form/div/textarea')

input_comment.send_keys(comment)

driver.find_element('xpath','/html/body/div[2]/div/div/div[2]/div/div/div[1]/div[1]/div[2]/section/main/div/div[1]/div/div[2]/div/div[4]/section/div/form/div/div[2]/div').click()

 

'파이썬' 카테고리의 다른 글

41. 판다스(Pandas)  (0) 2024.05.24
40. 넘파이(Numpy)  (1) 2024.05.23
38. 셀레니움-구글,네이버웹툰  (0) 2024.05.21
37. 크롤링과 스크레이핑  (0) 2024.05.20
27. 재귀 호출  (0) 2024.03.29