본문 바로가기

Python

Python - 홈페이지 탐색(with Selenium)

# Selenium을 이용하여 

# 홈페이지 탐색(with Selenium)

 

# 콘솔창에 !pip install selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import urllib.request

 

# chromedirver.exe 파일의 위치

path = "C:/User/Pyth/workspace/chromedriver"

 

# 크롤링할 사이트 주소

source_url = "http://madup.com"
driver = webdriver.Chrome(path)
driver.get(source_url)

 

# 1. 매드업 홈페이지의 상품소개서 열기 
driver.find_element_by_css_selector(".fa.fa-download").click()

# 2. 매드업 홈페이지 내의 이미지 다운로드 받기 

# 첫번째 이미지만 저장하기

driver = webdriver.Chrome()

driver.get("http://madup.com")

time.sleep(2)

img = driver.find_elements_by_css_selector(".img-mobile")[0].get_attribute('src')
urllib.request.urlretrieve(img,'test.jpg')

 

# 반복문으로 모든 이미지 저장하기

driver = webdriver.Chrome()

driver.get("http://madup.com")

time.sleep(2)

images = driver.find_elements_by_css_selector(".img-mobile")
count = 0
for image in images:
    count = count + 1
    for i in (0,len(driver.find_elements_by_css_selector(".img-mobile"))):
        i = image.get_attribute('src')
        urllib.request.urlretrieve(i,str(count)+'.jpg')

저장된 이미지 파일들 1~9
1
2
3

 

4
5
6
7
8
9

# 3. 매드업 홈화면을 캡쳐하여 이미지 파일로 저장하기
from selenium import webdriver
url = "http://madup.com"
driver = webdriver.Chrome("chromedriver")
driver.get(url)
driver.save_screenshot("madpup_home.png")
driver.quit()

저장된 madup_home 스크린 캡쳐 png파일
madup_home.png

 

 

카카오맵 클로링 포트폴리오 링크입니다. 

-> https://dsms27.tistory.com/93