IT 전용글/Python

BeautifulSoup 사용법 find, find_all

회상형인간 2021. 12. 24. 13:20

 

 

find_all 및 find
① find_all 은 해당 조건에 맞는 모든 태그를 가져옵니다.

 
1
find_all('태그명', {'속성명' : '값' ...})

② find 는 해당 조건에 맞는 첫 번째 태그를 가져옵니다.

 
1
find('태그명', {'속성명' : '값' ...})

③ select() : css 선택자를 사용해 값을 가져옵니다. class 앞에는.(점)을 사용, id앞에는 #(샵)을 사용

 
 
select(.class_name)
select(#id_name)

 

 

 

샘플 웹소스
 1) 샘플 웹소스를 D:\Python\test.html 등으로 작성합니다.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>    
<html>    
<head>    
     <title>BeautifulSoup 테스트</title>    
     <style>    
     table {     border: 1px solid black; }    
     a:link { text-decoration: none; }    
     #book_title { border: 2px solid blue; border-radius: 4px; width:50%; }    
     form {     border: 1px solid black; padding: 8px; }    
     div.reply { border: 1px solid #CCCCCC; }    
     </style>    
</head>    
    
<body>    
<h1>BeautifulSoup 테스트 페이지 입니다.</h1>    
<p><b>table 태그로 작성</b></p>    
     <table style="width:100%">    
     <tr>    
     <th>번호</th>    
     <th>도서제목</th>    
     <th>저자</th>    
     </tr>    
     <tr>    
     <td>1</td>    
     <td id="td_title"><a href="#">제로투원</a></td>    
     <td id="td_author">피터틸</td>    
     </tr>    
     <tr>    
     <td>2</td>    
     <td id="td_title"><a href="#">자기경영노트</a></td>    
     <td id="td_author">피터드러커</td>    
     </tr>    
     <tr>    
     <td>3</td>    
     <td id="td_title"><a href="#">시간을 정복한 남자, 류비셰프</a></td>    
     <td id="td_author">다닐 알렉산드로비치 그라닌</td>    
     </tr>    
     </table>    
     <br>    
     <form>    
     <label for="l_book_title">도서제목 : </label>    
     <input type="text" id="book_title" name="book_title">    
     <label for="l_author">저자 : </label>    
     <input type="text" id="author" name="author">    
     <input type="submit" value="입력">    
     </form>    
    
<p><b>div 태그로 작성</b></p>    
     <div>    
     <p>기본정보 위치하는 곳</p>    
     </div>    
     <div class="reply">    
     <p>대댓글입니다.</p>    
     <p>댓글입니다.</p>    
     </div>    
</body>    
</html>

 

 BeautifulSoup 활용

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
>>> from bs4 import BeautifulSoup
 
>>> with open("D:\Python/test.html", encoding='UTF8') as fp:
...     soup = BeautifulSoup(fp,'html.parser')
 
>>> soup.title
<title>BeautifulSoup 테스트</title>
 
>>> soup.title.string
'BeautifulSoup 테스트'
 
>>> soup.find_all('tr')
'생략'
 
>>> soup.find_all('td')
'생략'
 
>>> soup.find_all('td', {'id':'td_title'})
[<td id="td_title"><a href="https://google.com">제로투원</a></td>, <td id="td_title"><a href="https://daum.net">자기경영노트</a></td>, <td id="td_title"><a href="https://naver.com">시간을 정복한 남자, 류비셰프</a></td>]
 
>>> soup.find('div')
<div><p>기본정보 위치하는 곳</p></div>
 
>>> soup.h1
<h1>BeautifulSoup 테스트 페이지 입니다.</h1>
 
>>> soup.select('#td_author')
[<td id="td_author">피터틸</td>, <td id="td_author">피터드러커</td>, <td id="td_author">다닐 알렉산드로비치 그라닌</td>]
 
>>> soup.select('#td_author')[0].string
'피터틸'
 
>>> soup.select('.reply')[0].get_text()
'\n대댓글입니다.\n댓글입니다.\n\t\tp 태그 없는 경우\n\t'