개발일지/웹개발

웹스크래핑, mongoDB 연습

E-room 2022. 7. 6. 11:09
728x90

네이버의 영화 순위를 가져와서 필요한 정보만 데이터베이스에 저장해보자

import requests     				# requests 를 임포트합니다.
from bs4 import BeautifulSoup       # bs4 를 임포트합니다.
from pymongo import MongoClient 	# pymongo를 임포트합니다.
client = MongoClient("mongodb://localhost:27017/") # 로컬 주소
db = client['e-room'] # 데이터베이스 이름

# 타겟 URL을 읽어서 HTML를 받아오고,
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)

# HTML을 BeautifulSoup이라는 라이브러리를 활용해 검색하기 용이한 상태로 만듦
# soup이라는 변수에 "파싱 용이해진 html"이 담긴 상태가 됨
# 이제 코딩을 통해 필요한 부분을 추출하면 된다.
soup = BeautifulSoup(data.text, 'html.parser')

#############################
# (입맛에 맞게 코딩)
#############################

#old_content > table > tbody > tr:nth-child(2) > td:nth-child(1) > img  <= rank
#old_content > table > tbody > tr:nth-child(2) > td.title > div > a     <= title
#old_content > table > tbody > tr:nth-child(2) > td.point               <= point

movies = soup.select('#old_content > table > tbody > tr')

for movie in movies:
    rank = movie.select_one('td:nth-child(1) > img')
    if rank != None:
        rank = movie.select_one('td:nth-child(1) > img')["alt"]
        title = movie.select_one('td.title > div > a')["title"]
        star = movie.select_one('td.point').text
        doc = {
            "rank" : rank,
            "title" : title,
            "star" : star
        }
        db.movies.insert_one(doc) # movies collection에 저장한다.
        						  # movies 라는 collection이 없으면 자동 생성됨.

e-room.movies에 잘 저장된 모습

 

 

 

저장한 데이터를 꺼내보자

from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017/")
db = client['e-room']

movie = db.movies.find_one({'title':'밥정'},{'_id':False})

print(movie)

 

 

30위와 별점이 같은 영화는 몇개인가 ?

from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017/")
db = client['e-room']

star = db.movies.find_one({"rank" : "30"})["star"]

stars = list(db.movies.find({"star" : star}))

print(len(stars))
728x90