python을 기본 언어로 하여 크롤링한 정보를 google spreadsheet에 연동을 해보는 프로젝트 이며, 해당 포스팅에서는 크롤링을 만드는 과정을 정리했다.
GPT가 알려준다고해도 오래된 정보나 잘못된 정보를 알려줄 수 있기 때문에 중간중간 내가 따로 검색해서 해결하는 것이 좋다.

1. selenium을 이용하여 크롤링 하기

그냥 GPT한테 냅다 하고 싶은 것을 말한다. image image

GPT가 알려준 방식은 아래와 같다.

  1. selenium 설치
  2. Chrome webdriver 다운로드 및 설정
  3. 코드 작성

Selenium 설치

pip install selenium

Chrome WebDriver 설정

  1. 사용중인 크롬 버젼 확인
    크롬 버젼은 chrome://settings/help 으로 접속하면 확인 가능하다.

  2. 버전에 맞춰서 다운로드
    다운로드 페이지에서 크롬 웹 드라이버의 버전을 선택하여 다운로드
    사이트 - 구글 크롬 개발자 사이트 또는 Selenium 공식 웹사이트

  3. driver 설정

     from selenium import webdriver
     from selenium.webdriver.chrome.service import Service
    
     service = Service(executable_path='{다운로드 받은 경로}')
    
    • 원래 GPT가 알려준 코드는 아래와 같았는데 webdriver.Chrome가 예전 코드라 에러가 떠서 위와 같이 변경했다.(이 에러는 내가 따로 검색해서 해결하고 GPT한테 알려줬다.)
      from selenium import webdriver
      
      driver = webdriver.Chrome(executable_path='{다운로드 받은 경로}')
      

크롤링 하기

  • 사이트 선정
    나는 오늘의 집 사이트를 좋아해서 망설임 없이 오늘의 집 사이트의 콘텐츠의 사진과 제목을 긁어오는 코드로 작성해달라고 요청해봤다. image

  • 코드 초안 작성
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    
    service = Service(executable_path='/Desktop/Development/selenium/chromedriver')
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(service=service, options=options)
    
    driver.get('https://ohou.se/projects?writer=self')
    
    results = []
    
    # 각 콘텐츠의 링크를 크롤링
    content_links = [element.get_attribute('href') for element in driver.find_elements_by_css_selector('.project-feed__item a')]
    
    # 각 링크를 방문하여 사진과 콘텐츠 제목 크롤링
    for link in content_links:
        print(link)
        driver.get(link)
        title = driver.find_elements(By.CSS_SELECTOR,'h1').text
        image_url = driver.find_element_by_css_selector('.content-image img').get_attribute('src')
        results.append((title, image_url))
    
    print(results)
    driver.quit()
    
    • 위 코드를 실행하면 두개의 에러가 뜬다.
      1. 오래된 코드 image
        해당 에러도 find_elements_by_css_selector 가 오래된 코드라서 그렇다. find_elements(By.CSS_SELECTOR, ...) 으로 변경해주면 에러 없이 잘 돌아간다.
      2. title이랑 image_url 이 배열
        아래와 같이 변경해줘 보자
          title_element = driver.find_elements(By.CSS_SELECTOR,'h1')
          # 타이틀 존재 여부에 따른 조건문
          if title_element: 
           first_title = title_element[0].text
          else:
         first_title = "No Title"
          image_elements = driver.find_elements(By.CSS_SELECTOR,'.e1tspjql2')
          # 이미지 존재 여부에 따른 조건문
          if image_elements:
         first_image_url = image_elements[0].get_attribute('src')
          else:
         first_image_url = "No Image"
        
  • 수정 코드
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    
    service = Service(executable_path='/Desktop/Development/selenium/chromedriver')
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(service=service, options=options)
    
    driver.get('https://ohou.se/projects?writer=self')
    
    results = []
    
    # 각 콘텐츠의 링크를 크롤링
    content_links = [element.get_attribute('href') for element in driver.find_elements(By.CSS_SELECTOR, '.project-feed__item a')]
    
    # 각 링크를 방문하여 사진과 콘텐츠 제목 크롤링
    for link in content_links:
      driver.get(link)
      title_element = driver.find_elements(By.CSS_SELECTOR,'h1')
      # 타이틀 존재 여부에 따른 조건문
      if title_element:  
          first_title = title_element[0].text
      else:
        first_title = "No Title"
    
      image_elements = driver.find_elements(By.CSS_SELECTOR,'.e1tspjql2')
      # 이미지 존재 여부에 따른 조건문
      if image_elements:  
        first_image_url = image_elements[0].get_attribute('src')
      else:
        first_image_url = "No Image"  
      results.append((first_title, first_image_url))
      print((first_title, first_image_url))
    
    print(results)
    driver.implicitly_wait(10)
    driver.quit()
    
    • 결과 crawling gif

google streadsheet에 연동하는 것은 다음 포스팅에 작성해야할 것 같다