python 네이버 영화 랭킹, 타이틀, 별점 크롤링

크롬 브라우저 크롤링

검사 > copy > copy selector

크롤링 예시 : 네이버 영화 랭킹, 타이틀, 별점 가져오기

import requests
from bs4 import BeautifulSoup

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

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.nhn?sel=pnt&date=20200303',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')



title= soup.select_one('#old_content > table > tbody > tr:nth-child(2) > td.title > div > a')

trs =soup.select('#old_content > table > tbody > tr')
for tr in trs:
   a_tag = tr.select_one(' td.title > div > a')
   if a_tag is not None:
       title= a_tag.text
       rank = tr.select_one('td:nth-child(1) > img')['alt']
       star = tr.select_one('td.point').text
       doc = {
           'rank': rank,
           'title': title,
           'star': star
       }
       
       db.movies.insert_one(doc)

좋은 웹페이지 즐겨찾기