AI(Copilot)와 파이썬으로 수익 자동화 만들기 - (2) 코파일럿 사용방법

2023. 7. 17. 17:09신기술 기록 보관소

반응형

안녕하세요, 저는 AI Copilot을 사용하는 파이썬 개발자입니다. 오늘은 AI Copilot을 이용하여 간단하게 웹사이트에서 데이터를 수집하고 분석하는 크롤링 프로그램을 만들어보고, 그 결과를 수익화하는 방법에 대해 소개하겠습니다.


크롤링이란 웹사이트에서 원하는 정보를 자동으로 추출하는 과정입니다. 예를 들어, 네이버 뉴스에서 특정 키워드가 포함된 기사들의 제목과 내용을 가져오거나, 쇼핑몰에서 상품의 가격과 리뷰를 수집하는 것이 크롤링의 예입니다. 

크롤링을 통해 얻은 데이터는 다양한 용도로 활용할 수 있습니다. 예를 들어, 데이터 분석을 통해 시장 동향이나 고객 선호도를 파악하거나, 데이터 시각화를 통해 인사이트를 도출하거나, 데이터 기반의 서비스나 콘텐츠를 제공하는 것이 가능합니다.

하지만 크롤링을 하려면 웹사이트의 구조와 HTML 태그를 이해하고, 파이썬의 requests나 BeautifulSoup과 같은 라이브러리를 사용할 줄 알아야 합니다. 또한, 웹사이트가 자주 변경되거나, 다양한 형식의 데이터를 다루거나, 크롤링 속도나 안정성을 높이려면 추가적인 작업이 필요합니다. 

이런 과정들은 크롤링을 하는데 많은 시간과 노력을 요구합니다.

그래서 저는 AI Copilot을 사용하여 크롤링을 하는 방법을 찾았습니다. AI Copilot은 인공지능(AI) 기반의 코드 생성 서비스입니다. AI Copilot은 사용자가 입력한 자연어에 따라 적절한 파이썬 코드를 생성해줍니다.

 예를 들어, "네이버 뉴스에서 'AI'가 포함된 기사들의 제목과 URL을 가져와라"라고 입력하면, AI Copilot은 다음과 같은 코드를 생성해줍니다.

 

# Import requests and BeautifulSoup
import requests
from bs4 import BeautifulSoup

# Define the URL of the website
url = "https://news.naver.com/main/search/search.naver?query=AI&where=news&ie=utf8"

# Send a GET request to the website and get the HTML content
response = requests.get(url)
html = response.text

# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(html, "html.parser")

# Find all the elements that contain the article title and URL
articles = soup.find_all("a", class_="info")

# Loop through the articles and print the title and URL
for article in articles:
title = article.text # Get the text of the article title
link = article["href"] # Get the value of the href attribute of the article link
print(title, link) # Print the title and link

AI Copilot은 이렇게 간단한 명령으로도 원하는 웹사이트에서 데이터를 크롤링할 수 있는 코드를 생성해줍니다.

뿐만 아니라, AI Copilot은 다양한 웹사이트와 데이터 형식에 대응할 수 있으며, 크롤링 속도와 안정성을 높이기 위한 코드도 추가해줍니다. 예를 들어, "네이버 쇼핑에서 '에어팟 프로' 상품의 가격과 평점을 가져와라.

단, 상품은 100개만 가져오고, 크롤링 간격은 1초로 하라"라고 입력하면, AI Copilot은 다음과 같은 코드를 생성해줍니다.

 

 

 

# Import requests, BeautifulSoup and time
import requests
from bs4 import BeautifulSoup
import time

# Define the URL of the website
url = "https://search.shopping.naver.com/search/all?query=%EC%97%90%EC%96%B4%ED%8C%9F+%ED%94%84%EB%A1%9C"

# Define the number of products to crawl
num_products = 100

# Define the crawling interval in seconds
interval = 1

# Create an empty list to store the product data
products = []

# Loop until the number of products is reached or there is no more page to crawl
while len(products) < num_products:
# Send a GET request to the website and get the HTML content
response = requests.get(url)
html = response.text

# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(html, "html.parser")

# Find all the elements that contain the product information
items = soup.find_all("li", class_="basicList_item__2XT81")

# Loop through the items and get the product data
for item in items:
# Get the price of the product
price = item.find("span", class_="price_num__2WUXn").text

# Get the rating of the product
rating = item.find("span", class_="rating_star__3oqWX")
if rating: # If there is a rating element, get the text of it
rating = rating.text
else: # If there is no rating element, set the rating as None
rating = None

# Store the product data as a dictionary
product = {"price": price, "rating": rating}

# Append the product data to the list
products.append(product)

# Check if the number of products is reached
if len(products) == num_products:
break # Break the inner loop

# Check if there is a next page to crawl
next_page = soup.find("a", class_="pagination_next__1ITTf")
if next_page: # If there is a next page element, get the URL of it
url = next_page["href"]
else: # If there is no next page element, stop crawling
break # Break the outer loop

# Wait for the crawling interval
time.sleep(interval)

# Print the number of products crawled
print(f"Crawled {len(products)} products")

# Print the product data
for product in products:
print(product)

AI Copilot은 이렇게 복잡하고 세부적인 명령에도 정확하고 효율적인 코드를 생성해줍니다. AI Copilot은 크롤링뿐만 아니라 다른 파이썬 프로젝트에도 적용할 수 있습니다. 예를 들어, 데이터 분석, 데이터 시각화, 머신러닝, 웹 개발 등 다양한 주제와 목적에 맞는 코드를 생성해줄 수 있습니다.

AI Copilot을 사용하면 크롤링을 하는데 필요한 시간과 노력을 크게 줄일 수 있습니다. 또한, AI Copilot이 생성한 코드는 사용자가 원하는대로 수정하거나 확장할 수 있습니다. 예를 들어, 크롤링한 데이터를 저장하거나 전처리하거나 분석하거나 시각화하는 코드를 추가할 수 있습니다.

크롤링을 통해 얻은 데이터는 수익화하는 방법도 있습니다. 예를 들어, 크롤링한 데이터를 기반으로 한 뉴스레터나 블로그나 유튜브 채널을 운영하거나, 크롤링한 데이터를 판매하거나 공유하는 플랫폼을 만들거나, 크롤링한 데이터를 활용하는 서비스나 앱을 개발하는 것이 가능합니다. 

반응형