반응형
Python BeautifulSoup4 Parser
BeautifulSoup4는 HTML 또는 XML 문서를 파싱하여 데이터를 쉽게 추출할 수 있도록 도와주는 Python 라이브러리입니다.
1. BeautifulSoup Parser 종류
| Parser | 사용 예시 | 장점 | 단점 |
| Python html.parser | BeautifulSoup(markup, "html.parser") | - Python 기본 내장 - 별도 설치 불필요 - 적당한 속도) |
- lxml보다 느림 - html5lib보다 관용성이 낮음 |
| lxml HTML parser | BeautifulSoup(markup, "lxml") | - 매우 빠른 속도 - 관용적인 HTML 파싱 |
- 외부 C 라이브러리 필요 |
| lxml XML parser | BeautifulSoup(markup, "lxml-xml") BeautifulSoup(markup, "xml") |
- 매우 빠른 속도 - XML 파싱 지원 |
- 외부 C 라이브러리 필요 |
| html5lib | BeautifulSoup(markup, "html5lib") | - 가장 관대한 파싱 - 브라우저와 동일한 방식으로 HTML 파싱 |
- 속도가 매우 느림 - 외부 Python 라이브러리 필요 |
출처-https://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser
2. Parser 설치 방법
BeautifulSoup는 기본적으로 Python 내장 parser(html.parser) 를 사용할 수 있지만, 더 빠른 성능을 위해 lxml 사용을 권장합니다.
BeautifulSoup 설치
pip install beautifulsoup4
lxml 설치 (권장)
pip install lxml
html5lib 설치
pip install html5lib
3. Parser 사용 예제
from bs4 import BeautifulSoup
html = "<html><body><h1>Hello</h1></body></html>"
soup = BeautifulSoup(html, "lxml")
print(soup.h1.text)
참고URL
- pypi beautifulsoup4 : https://pypi.org/project/beautifulsoup4/
- Beautiful Soup Documentation : https://www.crummy.com/software/BeautifulSoup/bs4/doc/
728x90
반응형
'스크립트' 카테고리의 다른 글
| [python] 로또 번호 생성기 (0) | 2023.03.07 |
|---|---|
| What Is My IP?(myip) (0) | 2023.02.03 |
| [코딩테스트 입문] n의 배수 고르기 (0) | 2022.12.02 |
| [코딩테스트 입문] 자릿수 더하기 (0) | 2022.12.02 |
| [코딩테스트 입문] 짝수와 홀수 (0) | 2022.11.30 |