๐Ÿ Python ๐ŸŒฟ ์ค‘๊ธ‰

Python ์›น ํฌ๋กค๋ง

BeautifulSoup, Selenium์œผ๋กœ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘

โฑ๏ธ 6์‹œ๊ฐ„ ๐Ÿ‘๏ธ 20 ์กฐํšŒ
๐Ÿ“– ๊ฐ•์ขŒ ๋‚ด์šฉ
# Python ์›น ํฌ๋กค๋ง ๊ฐ€์ด๋“œ

## ํ•™์Šต ๋ชฉํ‘œ
์›น์‚ฌ์ดํŠธ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™์œผ๋กœ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค.

## 1. ํฌ๋กค๋ง์ด๋ž€?
์›นํŽ˜์ด์ง€์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™ ์ˆ˜์ง‘ํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.
- ๊ฐ€๊ฒฉ ๋น„๊ต
- ๋‰ด์Šค ์ˆ˜์ง‘
- ๋ฆฌ๋ทฐ ๋ถ„์„
- ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘

## 2. ํ•„์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
pip install requests beautifulsoup4 selenium

## 3. requests๋กœ HTML ๊ฐ€์ ธ์˜ค๊ธฐ
import requests

response = requests.get("https://example.com")
response.status_code # 200
response.text # HTML ๋‚ด์šฉ
response.encoding = "utf-8"

ํ—ค๋” ์„ค์ •:
headers = {"User-Agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers)

## 4. BeautifulSoup ํŒŒ์‹ฑ
from bs4 import BeautifulSoup

soup = BeautifulSoup(response.text, "html.parser")

ํƒœ๊ทธ๋กœ ์ฐพ๊ธฐ:
soup.find("h1")
soup.find_all("p")
soup.find("div", class_="content")
soup.find("div", id="main")
soup.find("a", href=True)

## 5. CSS ์„ ํƒ์ž
soup.select("div.content")
soup.select("#main")
soup.select("div > p")
soup.select("ul li")
soup.select("a[href]")
soup.select_one("h1")

## 6. ๋ฐ์ดํ„ฐ ์ถ”์ถœ
ํ…์ŠคํŠธ:
element.text
element.get_text()
element.get_text(strip=True)

์†์„ฑ:
element["href"]
element["src"]
element.get("class")

## 7. ์‹ค์ „ ์˜ˆ์ œ - ๋‰ด์Šค ํฌ๋กค๋ง
import requests
from bs4 import BeautifulSoup

url = "https://news.example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

articles = []
for item in soup.select("div.news-item"):
title = item.select_one("h2").text.strip()
link = item.select_one("a")["href"]
date = item.select_one(".date").text.strip()
articles.append({"title": title, "link": link, "date": date})

## 8. Selenium (๋™์  ํŽ˜์ด์ง€)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://example.com")

๋Œ€๊ธฐ:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "content"))
)

์š”์†Œ ์ฐพ๊ธฐ:
driver.find_element(By.ID, "search")
driver.find_element(By.CLASS_NAME, "btn")
driver.find_element(By.CSS_SELECTOR, "div.content")
driver.find_elements(By.TAG_NAME, "a")

์ž…๋ ฅ/ํด๋ฆญ:
element.send_keys("๊ฒ€์ƒ‰์–ด")
element.click()
element.clear()

์Šคํฌ๋กค:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")

HTML ๊ฐ€์ ธ์˜ค๊ธฐ:
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")

driver.quit()

## 9. ํŽ˜์ด์ง€๋„ค์ด์…˜
page = 1
all_data = []

while page <= 10:
url = f"https://example.com/list?page={page}"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

items = soup.select(".item")
if not items:
break

for item in items:
all_data.append(item.text)

page += 1
time.sleep(1) # ์„œ๋ฒ„ ๋ถ€ํ•˜ ๋ฐฉ์ง€

## 10. ๋ฐ์ดํ„ฐ ์ €์žฅ
CSV:
import csv
with open("data.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["title", "link"])
writer.writeheader()
writer.writerows(articles)

JSON:
import json
with open("data.json", "w", encoding="utf-8") as f:
json.dump(articles, f, ensure_ascii=False, indent=2)

Pandas:
import pandas as pd
df = pd.DataFrame(articles)
df.to_csv("data.csv", index=False)

## 11. ์—๋Ÿฌ ์ฒ˜๋ฆฌ
import time

for url in urls:
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
# ํŒŒ์‹ฑ
except requests.RequestException as e:
print(f"Error: {e}")
continue
finally:
time.sleep(1)

## 12. ์œค๋ฆฌ์  ํฌ๋กค๋ง
robots.txt ํ™•์ธ:
https://example.com/robots.txt

์š”์ฒญ ๊ฐ„๊ฒฉ ๋‘๊ธฐ:
import time
time.sleep(1)

User-Agent ์„ค์ •:
headers = {"User-Agent": "MyBot/1.0"}

์ €์ž‘๊ถŒ ์ค€์ˆ˜:
- ์ˆ˜์ง‘ ๋ฐ์ดํ„ฐ ์ƒ์—…์  ์‚ฌ์šฉ ์ฃผ์˜
- ์„œ๋ฒ„ ๋ถ€ํ•˜ ์ตœ์†Œํ™”
- ์ด์šฉ์•ฝ๊ด€ ํ™•์ธ

## ์ถ”์ฒœ ํ•™์Šต ์ˆœ์„œ
1. requests + BeautifulSoup ๊ธฐ์ดˆ
2. CSS ์„ ํƒ์ž ํ™œ์šฉ
3. Selenium ๋™์  ํŽ˜์ด์ง€
4. ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ฒ˜๋ฆฌ
5. ์—๋Ÿฌ ์ฒ˜๋ฆฌ์™€ ์ €์žฅ