
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
client = MongoClient(
'mongodb+srv://test:sparta@cluster0.55vah.mongodb.net/Cluster0?retryWrites=true&w=majority')
db = client.dbsparta
# 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 = BeautifulSoup(data.text, 'html.parser')
# select를 이용해서, tr들을 불러오기
trs = soup.select('#old_content > table > tbody > tr')
# movies (tr들) 의 반복문을 돌리기
for tr in trs:
# movie 안에 a 가 있으면,
a = tr.select_one('td.title > div > a')
if a is not None:
title = a.text
rank = tr.select_one('td:nth-child(1) > img')['alt']
star = tr.select_one('td.point').text
doc = {
'title': title,
'rank': rank,
'star': star
}
db.movies.insert_one(doc)

뭐가 문제일까요... 어제 부터 계속 mongodb에 연결하는 문제가 발생하네요...
