파이썬으로 웹 크롤링하는 가장 기본적인 예제

import requests
from bs4 import BeautifulSoup

# 1. 크롤링할 URL 설정
url = 'https://example.com'

# 2. 웹 페이지 요청 (HTTP GET)
response = requests.get(url)

# 3. 응답 상태 코드 확인
if response.status_code == 200:
    # 4. BeautifulSoup 객체 생성
    soup = BeautifulSoup(response.text, 'html.parser')

    # 5. 원하는 데이터 추출 (예: <p> 태그의 모든 내용 가져오기)
    paragraphs = soup.find_all('p')
    for p in paragraphs:
        print(p.get_text())
else:
    print('웹 페이지를 가져오지 못했습니다.')

TAGS.

Comments