- 스크래핑에 필요한 라이브러리 호출하기
import requests from bs4 import BeautifulSoup
- 예시 사이트에 요청을 진행, 응답을 바탕으로 BeautifulSoup 객체 생성하기
res = requests.get("http://books.toscrape.com/catalogue/category/books/travel_2/index.html") soup = BeautifulSoup(res.text, "html.parser")
- 태그에 해당하는 요소 모두 찾아보기
h3_results = soup.find_all("h3") len(h3_results) h3_results[0]
- booklist 에서 원하는 제목(title) 만 추출하기
for book in h3_results: print(book.a["title"])