새벽_나그네
12시 지난 새벽
새벽_나그네
전체 방문자
오늘
어제
  • 분류 전체보기
    • TIL
    • DevLog
    • Algorithm
    • ComputerScience
    • etc

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Github
  • 코딩프로젝트
  • 내일배움단
  • 자기주도학습
  • 내일배움캠프
  • Python
  • 코린이
  • 코딩
  • docker
  • portfolio
  • TodayILearned
  • AI트랙
  • Selenium 4
  • 개발일지
  • til
  • 국비지원
  • 스파르타코딩클럽
  • machine learning
  • 내일배움카드
  • 프로그래머스

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
새벽_나그네

12시 지난 새벽

TIL

1-14[Crawling, Selenium, Kaggle] Kaggle에 데이터 올리기, Selenium 이미지 스크래핑

2022. 1. 14. 23:00

오늘 할 일 (계획)

1. 머신러닝 프로젝트

 

2. Django 강의 시작

 

오늘 한 일 (회고)

1.  머신러닝 프로젝트

- Kaggle에 데이터 올리고 해당 파일을 가지고 인공지능 모델 구현

pokemon | Kaggle

 

pokemon

A dataset of images containing pokemon

www.kaggle.com

 

- Teachable machine으로 만든 인공지능 모델 URL 링크해서 json 파일 형태로 받아서 

업로드한 이미지에 맞게 결과값나오는 것 결과 확인 

-  코랩 사용하지 않은 Local 환경에서 인공지능 모델 이용하여 predict 함수 조절 테스트 중

 

2. Kaggle에 올릴 데이터 스크래핑용 스크래핑 코드 구현

 

* 코드 구현 요약

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import urllib.request
import os


driver = webdriver.Chrome()
driver.get("https://www.google.co.kr/imghp?hl=ko&tab=wi&authuser=0&ogbl")
elem = driver.find_element_by_name("q")

name = "ミュウ"

elem.send_keys(name)
elem.send_keys(Keys.RETURN)

path = '{}/{}'.format("./", name)

try:
    if not os.path.exists(path):
        os.makedirs(path)
except OSError:
    print("에러 ".format(path))


SCROLL_PAUSE_TIME = 1
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)
    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        try:
            driver.find_element_by_css_selector(".mye4qd").click()
        except:
            break
    last_height = new_height

images = driver.find_elements_by_css_selector(".rg_i.Q4LuWd")
count = 1


for image in images:
    try:
        image.click()
        time.sleep(2)
        imgUrl = driver.find_element_by_xpath('/html/body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div/div[1]/div[1]/div[2]/div/a/img').get_attribute("src")
        opener=urllib.request.build_opener()
        opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
        urllib.request.install_opener(opener)
        urllib.request.urlretrieve(imgUrl, '{}/{}/{}'.format("./", name,"/") + str(count) + ".jpg")
        count = count + 1
    except:
        pass

driver.close()
 
1. 사용을 하기위해서는 가상환경에서 실행을 해야한다.
2. 해당내용은 구글에서 검색할때 이미지의 위치를 기반으로 작성되어있다.
3. 검색어에따른 폴더를 만들어주는 넣어놓은 상태로 검색어만 바꿔주면 원하는 결과를 편하게 저장 가능하다.
 
path = '{}/{}'.format("./", name)
try:
    if not os.path.exists(path):
        os.makedirs(path)
except OSError:
    print("에러 ".format(path))
 
 

내일 할 일

1. 머신러닝 프로젝트 - 마무리 작업

 

2. Django 강의 시작

저작자표시 비영리 변경금지 (새창열림)

'TIL' 카테고리의 다른 글

1-16[Ajax, formData] Ajax를 이용한 이미지 데이터 전송  (0) 2022.01.16
1-15[Lecture] 생활코딩 장고 강의 학습  (0) 2022.01.15
1-13[Project] Tensor flow 작업 환경 구성  (0) 2022.01.13
1-12[Project] 첫번째 Machine learning 프로젝트 구상  (0) 2022.01.13
1-11[Machine learning] 2, 3, 4주차 개발일지  (0) 2022.01.11
    'TIL' 카테고리의 다른 글
    • 1-16[Ajax, formData] Ajax를 이용한 이미지 데이터 전송
    • 1-15[Lecture] 생활코딩 장고 강의 학습
    • 1-13[Project] Tensor flow 작업 환경 구성
    • 1-12[Project] 첫번째 Machine learning 프로젝트 구상
    새벽_나그네
    새벽_나그네
    IT, 프로그래밍, 정보, 스마트스토어

    티스토리툴바