똑똑한 HTML 분석기 - BeautifulSoup4 : 2-3. 원하는 요소 가져오기 I

임동윤·2022년 9월 27일
0

웹 스크래핑 기초

목록 보기
8/20
post-thumbnail

책 이름 모으기

  • 스크래핑에 필요한 라이브러리 호출하기
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"])

profile
AI Tensorflow Python

0개의 댓글