*1995년 ~2000년대 초반에 주로 사용되던 데이터 전달 방식
XML이란
예시
< ?xml version="1.0"? >
<고양이>
<이름>나비</이름>
<품종>샴</나이>
<중성화>예</중성화>
<발톱 제거>아니요</발톱 제거>
<등록 번호>12345</등록 번호>
<소유자>홍길동</소유자>
</고양이>
XML Parsing in Python
BeautifulSoup?
- HTML,XML등 Markup 언어 Scraping을 위한 대표적인 도구
- https://www.crummy.com/software/BeautifulSoup
- lxml과 html5lib과 같은 Parser를 사용함
- 속도는 상대적으로 느리나 간편히 사용할 수 있음
windows
1. cmd 실행
2. python -m install --upgrade pip
3. pip install beautifulsoup4
macOS
$ pip install beautifulsoup4
bs4 실습
from bs4 import BeautifulSoup
with open("books.xml","r",encoding="utf8") as books_file:
books_xml = books_file.read() #File을 String으로 읽어오기
#xml 모듈을 사용해서 데이터 분석
soup = BeautifulSoup(books_xml,"lxml")
#author가 들어간 모든 element 추출
for book_info in soup.find_all("author"):
print(book_info)
print(book_info.get_text())
실습하면서 bs4가 제대로 설치가 되지 않아
ModuleNotFoundError: No module named 'bs4'
오류가 계속나서 애를 먹었음.
결국 해당 프로젝트에
$pip install bs4 하여 해결.
또한 lxml이 설치가 되지 않아 lxml을 설치
$pip install lxml