콘솔워크

python html에서 원하는 태그만 추출하기 본문

프로그래밍/python

python html에서 원하는 태그만 추출하기

콘솔워크 2023. 5. 31. 23:26
반응형

beautifulsoup4 라이브러리를 설치한다.

pip install beautifulsoup4

 

from bs4 import BeautifulSoup


html_file = r"__test__\article_sample.html"
page = open(html_file, "rt", encoding="utf-8").read()  # HTML 파일 읽고 문자열 리턴
soup = BeautifulSoup(page, "html.parser")  # Soup 객체 생성

# div와 p 두 종류가 있음
for div in soup.find_all(["div", "p"]):
    print(div)

beautifulsoup4 라이브러리를 사용하면 html을 쉽게 파싱할 수 있다.

 

fild_all 함수를 이용하여 원하는 태그만 추출 가능하다.

 

 

반응형