몽고디비
몽고디비링크
몽고디비수정하기전
from pymongo import MongoClient
client = MongoClient('여기에 URL 입력')
db = client.dbsparta
doc = {
'name':'bob',
'age':27
}
db.users.insert_one(doc)
몽고디비수정후(추가해야할것과 certifi설치해야함)
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://test:비번@cluster0.qlxvjbm.mongodb.net/Cluster0?retryWrites=true&w=majority',tlsCAFile=ca)
db = client.dbsparta
doc = {
'name':'bob',
'age':27
}
db.users.insert_one(doc)
//오른쪽버튼 눌러서 실행해야함
몽고디비로 데이터넣기
db.users.insert_one({'name':'bobby','age':27})
db.users.insert_one({'name':'cha','age':17})
db.users.insert_one({'name':'choi','age':117})
몽고디비 데이터 꺼내오기
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://test:sparata@cluster0.qlxvjbm.mongodb.net/Cluster0?retryWrites=true&w=majority',tlsCAFile=ca)
db = client.dbsparta
all_users=list(db.users.find({}))
for user in all_users:
print(user)
몽고디비 데이터 꺼내오기2
//위에선 _id값이 보였지만 _id값을 빼고 보는법
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://test:sparata@cluster0.qlxvjbm.mongodb.net/Cluster0?retryWrites=true&w=majority',tlsCAFile=ca)
db = client.dbsparta
all_users=list(db.users.find({},{'_id':False}))
for user in all_users:
print(user)
몽고디비 자주쓰는 내용
# 저장 - 예시
doc = {'name':'bobby','age':21}
db.users.insert_one(doc) //db.db이름
# 한 개 찾기 - 예시
user = db.users.find_one({'name':'bobby'})
# 여러개 찾기 - 예시 ( _id 값은 제외하고 출력)
all_users = list(db.users.find({},{'_id':False}))
# 바꾸기 - 예시
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})
# 지우기 - 예시
db.users.delete_one({'name':'bobby'})
몽고디비에 영화크롤링 넣기
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient //몽고디비 설정값 넣고
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://test:sparata@cluster0.qlxvjbm.mongodb.net/Cluster0?retryWrites=true&w=majority',tlsCAFile=ca)
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')
title=soup.select_one('#old_content > table > tbody > tr:nth-child(2) > 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)
연습하기-가버나움의 평점(star)가져오기
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://test:sparata@cluster0.qlxvjbm.mongodb.net/Cluster0?retryWrites=true&w=majority',tlsCAFile=ca)
db = client.dbsparta
movie = db.movies.find_one({'title':'가버나움'}) //movies 디비안에 있으니
print(movie['star']) // movie안에 star하기
가버나움과 같은 평점인 영화가져오기
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://test:sparata@cluster0.qlxvjbm.mongodb.net/Cluster0?retryWrites=true&w=majority',tlsCAFile=ca)
db = client.dbsparta
movie = db.movies.find_one({'title':'가버나움'}) //일단 가버나움의 평점을 찾은후
star=movie['star']
all_movies = list(db.movies.find({'star':star},{'_id':False})) //여러개찾기를 활용함
for m in all_movies: //m은그냥 그룹만들어준거
print(m['title'])
가버나움 평점0으로 만들기
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://test:sparata@cluster0.qlxvjbm.mongodb.net/Cluster0?retryWrites=true&w=majority',tlsCAFile=ca)
db = client.dbsparta
db.movies.update_one({'title':'가버나움'},{'$set':{'star':'0'}}) //바꾸기활용할것