반응형

 

이번 프로젝트의 목표는 파이썬을 사용하여 아마존 웹사이트에서 원하는 제품 정보를 자동으로 수집하는 것입니다. 다음과 같은 데이터를 각 제품에 대해 수집할 예정입니다.

  • ASIN (Amazon Standard Identification Number): Amazon 제품을 고유하게 식별하는 번호.
  • Title (제품명): 제품의 이름 또는 제목.
  • Brand (브랜드): 제품의 브랜드명.
  • Amazon Choice: 해당 제품이 'Amazon's Choice'로 표시되어 있는지 여부.
  • Star Rating: 제품의 평균 별점.
  • Rating Count: 제품에 대한 고객 리뷰의 총 개수.
  • Rufus Question 2: 제품의 FAQ 섹션에서 두 번째 질문과 답변.
  • Coupon Discounts: 제품에 적용 가능한 쿠폰 할인 정보.
  • Scrape Time: 데이터를 스크랩한 시간.

웹 사이트를 스크래핑 하기 위해서는 특별한 라이브러리가 필요합니다. 

 

  • requests + BeautifulSoup → 정적 페이지 크롤링
  • Selenium → 동적 웹페이지 크롤링 (브라우저 자동화)
  • Scrapy → 대량 데이터 수집에 적합
  • Amazon Product Advertising API → 공식 API

첫 시도로 requests + BeautifulSoup를 사용하였으나, 아래와 같은 문제가 발생했습니다.

<body>
<!--
        To discuss automated access to Amazon data please contact api-services-support@amazon.com.
        For information about migrating to our APIs refer to our Marketplace APIs at https://developer.amazonservices.com/ref=rm_c_sv, or our Product Advertising API at https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html/ref=rm_c_ac for advertising use cases.
-->
<!--
Correios.DoNotSend
-->
<div class="a-container a-padding-double-large" style="min-width:350px;padding:44px 0 !important">
<div class="a-row a-spacing-double-large" style="width: 350px; margin: 0 auto">
<div class="a-row a-spacing-medium a-text-center"><i class="a-icon a-logo"></i></div>
<div class="a-box a-alert a-alert-info a-spacing-base">
<div class="a-box-inner">
<i class="a-icon a-icon-alert"></i>
<h4>Enter the characters you see below</h4>
<p class="a-last">Sorry, we just need to make sure you're not a robot. For best results, please make sure your browser is accepting cookies.</p>

 

 

아마존에서는 로봇 차단 시스템 (CAPTCHA) 을 사용하여 자동화된 접근을 차단하고 있습니다. 이러한 문제를 해결하기 위해서는 Selenium을 사용하여 실제 브라우저를 통해 데이터를 크롤링하거나, Amazon Product Advertising API를 활용하는 방법이 있습니다. Selenium을 활용한 웹 스크래핑은 차단될 위험이 있으므로, 헤더(User-Agent) 설정을 하여 봇이 아닌 것처럼 보이게 하거나 딜레이 적용을 하여 차단을 방지할 수 있습니다.

 

Selenium은 웹 브라우저를 자동으로 제어할 수 있는 파이썬 라이브러리입니다. 실제 웹 브라우저를 띄워서 사용자가 수동으로 웹페이지를 탐색하는 것처럼 자동화된 작업을 수행할 수 있게 합니다. 

# Import necessary packages
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from openpyxl import Workbook
from getpass import getpass
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

webdriver는 Selenium을 사용하여 실제 브라우저를 제어하는 주요 객체입니다. 이를 통해 Chrome, Firefox 등 브라우저를 자동으로 실행하고 제어할 수 있습니다. 

from selenium.webdriver.chrome.service import Service

Selenium에서 Chrome 브라우저를 실행할 때 필요한 서비스 객체를 임포트합니다. 이를 통해 chromedriver를 실행하는 데 사용됩니다.

from selenium.webdriver.common.by import By

By는 웹 페이지에서 특정 요소를 찾을 때 사용되는 방법을 정의합니다. 예를 들어, By.ID, By.CLASS_NAME 등을 사용하여 HTML 요소를 찾습니다.

from selenium.webdriver.common.keys import Keys

Keys는 키보드 입력을 자동화할 때 사용됩니다. 예를 들어, 텍스트 입력창에 Keys.ENTER를 사용해 엔터 키를 누를 수 있습니다.

from webdriver_manager.chrome import ChromeDriverManager

webdriver_manager는 Selenium을 사용할 때 필요한 ChromeDriver를 자동으로 설치하고 관리해주는 라이브러리입니다. 이를 사용하면 매번 수동으로 드라이버를 다운로드하지 않고 자동으로 최신 버전의 드라이버를 설치할 수 있습니다.

from selenium.webdriver.support.ui import WebDriverWait

WebDriverWait은 특정 요소가 로드되거나 특정 조건이 충족될 때까지 대기하는 기능을 제공합니다. 웹 페이지가 동적으로 로드될 때 유용합니다.

from selenium.webdriver.support import expected_conditions as EC

expected_conditions는 Selenium에서 사용할 수 있는 여러 가지 기대 조건들을 제공합니다. 예를 들어, 특정 요소가 클릭 가능할 때까지 기다리거나, 페이지 로딩이 완료될 때까지 기다리는 등의 조건을 설정할 수 있습니다.


다음 포스팅에서 이어집니다.

반응형
올리브한입