pymongo 웹스크래핑 저장하기

박서현·2023년 8월 7일
0
post-thumbnail
post-custom-banner
import requests
from bs4 import BeautifulSoup

URL = "https://movie.daum.net/ranking/reservation"
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(URL, headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')

from pymongo import MongoClient
client = MongoClient('')
db = client.dbsparta

lis = soup.select('#mainContent > div > div.box_ranking > ol > li')

for li in lis:
    title = li.select_one('.link_txt').text.replace(',','')
    rank = li.select_one('.rank_num').text
    rate = li.select_one('.txt_grade').text
    doc = {
        'title': title,
        'rank': rank,
        'rate': rate
    }
    db.movies.insert_one(doc)

post-custom-banner

0개의 댓글