언어&프레임워크
[웹개발종합반]웹스크렙핑(크롤링)기초
밍풀
2023. 2. 14. 10:44
네이버영화에 있는 영화제목과 별점 가져오기
스크렙프를 위한 라이브러리 필요함
; beautifulsoup
pip install bs4로 설치( venv로 되어있는지 체크 필수)
크롤링 ; 웹에 접속해서, 데이터를 솎아내어 가져오는것
필요한 라이브러리 1. 웹에 접속하는(requests) 2. 데이터를 솎아내는 용도(bs4)
데이터 솎아낼 준비하기
1
2
3
4
5
6
7
|
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
|
cs |
웹페이지에서 원하는 부분(그린북) 에서 오른쪽버튼 클릭=>검사=> copy selector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
a= soup.select_one('#old_content > table > tbody > tr:nth-child(3) > td.title > div > a')
#a에 그린북 해당 html코드 가져옴 <a href="/movie/bi/mi/basic.naver?code=171539" title="그린 북">그린 북</a>
#그린북이라는 텍스트만 가져오기
print(a.text)
#href안의 값, <>안의 속성값(여기선 url) 가져오기
print(a['href'])
|
cs |
제목들 가져오기
아이디어 ; html가 아래와 같은 형태이기에 tr을 가져와서 제목만 뽑아내기
아무거나 두개 제목부분 tr copyselect 한개 저 두개이고 모양이 비슷함을 알 수 있음
영화 웹페이지에서 제목들만 뽑아내기
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
|
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)
#요청
soup = BeautifulSoup(data.text, 'html.parser')
#솎아내기준비
trs=soup.select('#old_content > table > tbody > tr')
#tr 태그들만 뽑아냄, 리스트로 쌓여있는 상태, 여러개 뽑을땐 select 한개뽑을땐 select_one
#old_content > table > tbody > tr:nth-child(5) > td.title > div > a
#위에가 tr에 들어있는 영화제목부분이니까 아래와같이 솎아냄
for tr in trs :
a=tr.select_one('td.title > div > a')
#<a href="/movie/bi/mi/basic.naver?code=10016" title="나 홀로 집에">나 홀로 집에</a>와 같은 태그들 뽑아옴
#그런데 중간에 none(웹페이지 구분선 때문)이 있어서 아래와 같이 처리필요
if(a is not None):
print(a.text)
#제목들만 뽑아내게됨
|
cs |
순위와 별점도 출력해보기
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
|
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get('https://movie.naver.com/movie/sdb/rank/rmovie.naver?sel=pnt&date=20210829',headers=headers)
#요청
soup = BeautifulSoup(data.text, 'html.parser')
#솎아내기준비
trs=soup.select('#old_content > table > tbody > tr')
for tr in trs :
a=tr.select_one('td.title > div > a')
if(a is not None):
title = a.text
##old_content > table > tbody > tr:nth-child(6) > td:nth-child(1) > img
rank = tr.select_one('td:nth-child(1) > img')['alt']
#<img alt="23" height="13" src="https://ssl.pstatic.net/imgmovie/2007/img/common/bullet_r_g23.gif" width="14"/>
# 위와 같은 부분에서 alt 뽑아냄
star = tr.select_one('td.point').text
print(title, rank, star)
|
cs |
다른언어로는 크롤링 쉽지 않지만 파이썬은 라이브러리가 잘 되어있기에 가능한 것이라고 한다.....!!