BeautifulSoup 기본 사용


html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

예제로 사용할 html 문서(html_doc)가 있습니다. (BeautifulSoup 공식 문서 참고)


soup.prettify()

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

print(soup.prettify())
<head>
  <title>
   The Dormouse's story  </title> </head> <body>
  <p class="title">
   <b>
    The Dormouse's story
   </b>
  </p>
  <p class="story">
   Once upon a time there were three little sisters; and their names were
   <a class="sister" href="http://example.com/elsie" id="link1">
    Elsie
   </a>
   ,
   <a class="sister" href="http://example.com/lacie" id="link2">
    Lacie
   </a>
   and
   <a class="sister" href="http://example.com/tillie" id="link3">
    Tillie
   </a>
   ; and they lived at the bottom of a well.
  </p>
  <p class="story">
   ...
  </p>
 </body>
</html>

soup.prettify() 를 출력해보면 html 문서의 계층 구조를 알기 쉽게 보여줍니다.



soup.title

print(soup.title)
<title>The Dormouse's story</title>

title 태그를 반환합니다.



soup.title.name

print(soup.title.name)
title

title 태그의 이름(‘title’)을 반환합니다.



soup.title.string

print(soup.title.string)
The Dormouse's story

title 태그의 문자열을 반환합니다.



soup.title.parent.name

print(soup.title.parent.name)
head

title 태그의 부모 태그의 이름을 반환합니다.



soup.p

print(soup.p)
<p class="title"><b>The Dormouse's story</b></p>

첫 p 태그를 반환합니다.



soup.p[‘class’]

print(soup.p['class'])
['title']

‘class’ 속성이 있는 첫 p 태그를 반환합니다.



soup.a

print(soup.a)
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

a 태그를 반환합니다.



soup.find_all()

print(soup.find_all('a'))
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

모든 a 태그를 리스트 형태로 반환합니다.



soup.find()

soup.find(id="link3")
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

id가 ‘link3’인 태그를 반환합니다.



get()

for link in soup.find_all('a'):
    print(link.get('href'))
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie

href 속성을 반환합니다.



get_text()

print(soup.get_text())
The Dormouse's story

The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...

html 문서 안에 있는 텍스트를 반환합니다.


이전글/다음글