*이 글을 읽기전에 작성자 개인의견이 있으니, 다른 블로그와 교차로 읽는것을 권장합니다.*
1. 로그인
!pip install selenium
!pip install chromedriver_autoinstaller
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
url = 'https://www.instagram.com'
driver.get(url)
id= '인스타그램 아이디'
pw= '인스타그램 비밀번호'
input_id = driver.find_element('xpath', '/html/body/div[2]/div/div/div[2]/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div[1]/div/label/input')
input_pw = driver.find_element('xpath', '/html/body/div[2]/div/div/div[2]/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div[2]/div/label/input')
input_id.send_keys(id)
input_pw.send_keys(pw)
driver.find_element('xpath', '/html/body/div[2]/div/div/div[2]/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div[3]/button').click()
2. 해시태그 검색
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def hashtag(tag):
url = 'https://www.instagram.com/explore/tags/'+ tag
driver.get(url)
hashtag('맛점')
3. 스크롤 내리기
import time
for _ in range(1):
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[1]/div[1]/a/div[1]/div[2]').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[3]/div/div/section[1]/span[1]/div/div').click()
driver.find_element('xpath','/html/body/div[8]/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[3]/div/div/section[1]/span[1]/div/div').click()
driver.find_element('xpath','/html/body/div[8]/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[3]/div/div/section[1]/span[2]/div/div').click()
comment = driver.find_element('xpath','/html/body/div[8]/div[1]/div/div[3]/div/div/div/div/div[2]/div/article/div/div[3]/div/div/section[3]/div/form/div/textarea')
comment.send_keys('맛있어 보이네요')
6. 함수로 리팩토링
!pip install selenium
!pip install chromedriver_autoinstaller
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# 로그인
def login(id, pw):
input_id = driver.find_element('xpath', '/html/body/div[2]/div/div/div[2]/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div[1]/div/label/input')
input_pw = driver.find_element('xpath', '/html/body/div[2]/div/div/div[2]/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div[2]/div/label/input')
input_id.send_keys(id)
input_pw.send_keys(pw)
driver.find_element('xpath', '/html/body/div[2]/div/div/div[2]/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div[3]/button').click()
# 해시태그 검색
def searching(hashtag):
url = 'https://www.instagram.com/explore/tags/'+ hashtag
driver.get(url)
# 좋아요 및 댓글달기
# def like_and_comment(comment):
# xpath = ''
# driver.find_element('xpath':xpath).click()
# reply_xpath = ''
# driver.find_element('xpath':xpath).click()
# driver.find_element('xpath':xpath).send_keys(comment)
# 실행
driver = webdriver.Chrome()
url = 'https://www.instagram.com'
driver.get(url)
driver.implicitly_wait(3)
id= '인스타그램 아이디'
pw= '인스타그램 비밀번호'
login(id, pw)
time.sleep(4)
searching('사과')
# time.sleep(4)
# like_and_comment('잘보고 갑니다~'):
'Python > 크롤링(Crawling)' 카테고리의 다른 글
스타벅스 크롤링하기 + 차트 시각화 (0) | 2024.06.11 |
---|---|
파이썬(4)-pixabay (0) | 2024.05.23 |
파이썬(2)-Selenium, 동적사이트 크롤링 (0) | 2024.05.23 |
파이썬(1)-Crawling, Scraping (0) | 2024.05.20 |