0w0

[BeautifulSoup] soup.find_all(), soup.find(), prettify() 적용 본문

Coding/Python

[BeautifulSoup] soup.find_all(), soup.find(), prettify() 적용

0w0 2019. 8. 17. 12:35
728x90
반응형

 

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
#coding=utf-8
 
import sys, io, time
import urllib.request as urq
import urllib.parse
from bs4 import BeautifulSoup
 
#--구글 html 소스 파싱
html=urq.urlopen('https://www.google.com/').read()
 
#--파싱된 소스를 BeautifulSoup 적용
soup=BeautifulSoup(html,'html.parser')
 
print("#--태그로 find_all")
#모든 div 태그를 가져와 리스트 형태 저장
div_all=soup.find_all('div')
print(div_all[0])
for div in div_all: #print(all_div), print(div_all.prettify())
    print(div)      
    
 
print()
print()
 
 
print("#--태그로 find")
#첫번째 div 태그만 가져오게됨.
div_first=soup.find('div')
print(div_first)
print(div_first.prettify())
 
 
print()
print()
 
 
print("#--태그와 속성으로 특정 영역 find")
div_choice=soup.find('div',{'id':'guser'})
print(div_choice)
print(div_all[1])
print(div_choice.prettify())
 
 
cs

 

 

728x90
반응형
Comments