728x90
반응형
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')
# 코딩 시작
#old_content > table > tbody > tr:nth-child(3) > td.title > div > a
#old_content > table > tbody > tr:nth-child(4) > td.title > div > a
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
a = movie.select_one('td.title > div > a')
if a is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank, title, star)
기존에 웹 스크래핑으로 만들어 놓았던
2022.12.29 - [𝔻𝕖𝕧𝕖𝕝𝕠𝕡/웹 스크래핑 크롤링] - [웹 스크래핑 크롤링] 웹스크래핑(크롤링) 연습
해당 코드를 이용해보자
이 데이터를 MongoDB에 저장할거다
pymongo 기본코드 넣어주기
from pymongo import MongoClient
client = MongoClient('mongodb+srv://sparta:0000@cluster0.5fhqjdx.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
client = MongoClient('mongodb+srv://sparta:0000@cluster0.5fhqjdx.mongodb.net/?retryWrites=true&w=majority')
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.naver?sel=pnt&date=20210829',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
# 코딩 시작
#old_content > table > tbody > tr:nth-child(3) > td.title > div > a
#old_content > table > tbody > tr:nth-child(4) > td.title > div > a
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
a = movie.select_one('td.title > div > a')
if a is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
print(rank, title, star)
여기다가 프린트 할 이유는 없으니 print는 없애주자
그리고 컬렉션 넣는 방법은?
바로 전에 배운 내용이다.
저장코드 넣어주기
# 저장 - 예시
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)
기입해주자
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
client = MongoClient('mongodb+srv://sparta:0000@cluster0.5fhqjdx.mongodb.net/?retryWrites=true&w=majority')
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.naver?sel=pnt&date=20210829',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
# 코딩 시작
#old_content > table > tbody > tr:nth-child(3) > td.title > div > a
#old_content > table > tbody > tr:nth-child(4) > td.title > div > a
movies = soup.select('#old_content > table > tbody > tr')
for movie in movies:
a = movie.select_one('td.title > div > a')
if a is not None:
title = a.text
rank = movie.select_one('td:nth-child(1) > img')['alt']
star = movie.select_one('td.point').text
doc = {
'title':title,
'rank':rank,
'star':star
}
db.movies.insert_one(doc)
해당 코드를 입력하면
이런식으로 컬렉션에 데이터들이 DB로 들어간것을 볼 수 있다.
-끝-
728x90
반응형
'📁 𝐝𝐚𝐭𝐚𝐁𝐚𝐬𝐞 > MongoDB' 카테고리의 다른 글
[MongoDB] Quiz (4) | 2022.12.29 |
---|---|
[MongoDB] pymongo 라이브러리로 Mongo DB 조작하는 방법 (0) | 2022.12.29 |
[MongoDB] 연결하고 저장하기 (0) | 2022.12.29 |