ใ ์คํ๋ฅดํ์ฝ๋ฉํด๋ฝ ์น๊ฐ๋ฐ ์ข ํฉ โ1์ฃผ์ฐจโ ใ
ใ ์คํ๋ฅดํ์ฝ๋ฉํด๋ฝ ์น๊ฐ๋ฐ ์ข ํฉ โ2์ฃผ์ฐจโ ใ
[ SQL vs NoSQL ]
๐ SQL(๊ด๊ณํ DB / RDBMS)
- ๋ฐ์ดํฐ๋ฅผ ํ๊ณผ ์ด๋ก ๊ตฌ์ฑ๋ ์ ํํ๋ ํ ํํ์ ํ ์ด๋ธ๋ก ์ ์ฅ
- ๋ฐ์ดํฐ์ ์ข ์์ฑ์ ๊ด๊ณ๋ก ํํ
- ์ด์ ๋ฐ์ดํฐ์ ์์ฑ์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๊ณ , ํ์ ์ด(๋ฐ์ดํฐ ์์ฑ)์ ํ์์ ๋ง๋ ๋ฐ์ดํฐ๊ฐ ๋ด๊น
- ์ด์ ๊ตฌ์กฐ์ ๋ฐ์ดํฐ๊ฐ์ ๊ด๊ณ๊ฐ ํ ์ด๋ธ ์คํค๋ง๋ก ์ฌ์ ์ ์ ์๋์ด ์ฌ์ฉ
- ์คํค๋ง๋ฅผ ์์ ํ๊ธฐ๊ฐ ์ด๋ ต์ง๋ง, ์ ํํ ๋์ด ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ์ ๋ถ๋ฅ, ์ ๋ ฌ, ํ์ ์๋๊ฐ ๋น ๋ฆ
- ๋ํ์ ์ธ RDBMS : MySQL, Oracle, MariaDB...
๐ NoSQL(๋น๊ด๊ณํ DB)
- ๋จ์ด์ ๋ป์ "Not Only SQL"์ด๋ฉฐ, ๊ด๊ณํ DB๋ฅผ ์ ํ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋ ์ฌ๋ฌ ์ ํ์ DB๋ฅผ ์ฌ์ฉ
- ๋๋ถ๋ถ์ด ์คํ์์ค ํํ์ด๋ฉฐ, ์คํค๋ง ์์ด ๋์ํ๊ณ ๊ตฌ์กฐ์ ๋ํ ์ ์ ๋ณ๊ฒฝ ์์ด ์์ ๋กญ๊ฒ ํ๋ ์ถ๊ฐ ๊ฐ๋ฅ
๐ Key-Value ๋ชจ๋ธ
- ํค(Key)์ ๊ฐ(value)์ผ๋ก ์ด๋ฃจ์ด์ง ๋์ ๋๋ฆฌ ํํ์ ๊ฐ์ฅ ๋จ์ํ NoSQL
- ์ํ์ ํ์ฅ์ ์ฉ์ดํ๋ฉฐ ํค-๊ฐ ์ ์ฅ์๋ ๋ฐ์ดํฐ๊ฐ ํค์ ๊ฐ์ ์์ผ๋ก ์ ์ฅ๋์ด ์์
- ๊ฐ๋จํ ๋ฐ์ดํฐ ๋ชจ๋ธ์ ๋์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ฃผ ๋ถ๋ฌ์ค๊ณ ์ ์ฅํ๋ ๊ฒฝ์ฐ์ ์ ํฉ
- ๊ฐ์ ์ด๋ฏธ์ง๋ ๋น๋์ค ๋ฑ ์ด๋ ํ ํํ๋ผ๋ ๊ฐ๋ฅ, ๊ฐ๋จํ API๋ฅผ ์ ๊ณตํ๊ธฐ์ ์ง์ ์๋๊ฐ ๋น ๋ฅธ ํธ์
- ๋ํ์ ์ธ DBMS : Memcached, Redis, AWS Dynamo DB, LevelDB...
๐ Document ๋ชจ๋ธ
- Key-Value ๋ชจ๋ธ๊ณผ ๋์ผํ๊ฒ ๋ฐ์ดํฐ๋ฅผ ํค์ ๊ฐ์ผ๋ก ์ด๋ฃจ์ด์ง ํํ๋ก ์ ์ฅ
- Key-Value ๋ชจ๋ธ๊ณผ ๋ฌ๋ฆฌ Value๊ฐ ๊ณ์ธต์ ํํ์ ๋ฌธ์(JSON, XML๊ณผ ๊ฐ์ ํ์ค ํ์ ์ด์ฉ)๋ก ์ ์ฅ๋๋ฉฐ ์ด๋ ๊ฐ์ฒด์งํฅ์ ๊ฐ์ฒด์ ์ ์ฌํจ
- Value๋ ํ๋์ ๋จ์๋ก ์ทจ๊ธ๋์ด ์ ์ฅ ํ๊ธฐ ์ ์ ๋ณ๋๋ก ์คํค๋ง๋ฅผ ์ ์ํ์ง ์์ผ๋ฉฐ ๋ฌธ์ ์ถ๊ฐ์ ๊ทธ๊ฒ์ด ์คํค๋ง๊ฐ ๋จ
- ๋ํ์ ์ธ DBMS : MongoDB, CouchDB, MarkLogic...
๐ Column Family ๋ชจ๋ธ
- ์์ ๋ ๋ชจ๋ธ์ Key-Value ์์์ Value๋ฅผ ํตํด ํ๋๋ฅผ ๊ฒฐ์ ํ๋ค๋ฉด, Column Family ๋ชจ๋ธ์ Key๋ฅผ ํตํด ํ๋ ๊ฒฐ์
- Key๋ Row-Key์ Column-Family, Column-name์ ๊ฐ์ง๋ฉฐ ์ฐ๊ด๋ ๋ฐ์ดํฐ๋ค์ Column-Family์ ์ํด ์์ผ๋ฉฐ, ๊ฐ์์ Column-name์ ๊ฐ์ง
- ์ด๋ ๊ฒ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ ํ๋์ ํฐ ํ ์ด๋ธ๋ก ํํ์ด ๊ฐ๋ฅํ๋ฉฐ ์ง์๋ Row->Column-Family->Column-Name์ ํตํด ์ํ
- ๋ํ์ ์ธ DBMS : HBase, Cassandra, Hypertable, GCP BigTable...

๐ Graph ๋ชจ๋ธ
- ์ค์ ์ธ๊ณ์ ๋ฐ์ดํฐ๋ฅผ ๊ด๊ณ์ ํจ๊ป ํํํ๊ธฐ ์ํด ๊ณ ์๋ ๋ชจ๋ธ
- ๋ฐ์ดํฐ๋ ์ฐ์์ ์ธ ๋ ธ๋, ๊ด๊ณ, ํน์ฑ์ ํํ(๊ทธ๋ํ)๋ก ์ ์ฅ
- ๊ทธ๋ํ ๋ชจ๋ธ์์์ ์ง์๋ ๊ทธ๋ํ ์ํ๋ฅผ ํตํด ์ด๋ฃจ์ด์ง
[ MongoDB ]
- ๊ณต์ ํํ์ด์ง :https://www.mongodb.com/ko-kr
- NoSQL๋ก ๋ถ๋ฅ๋๋ ํฌ๋ก์ค ํ๋ซํผ Document ์งํฅ DBMS
- MongoDB๋ SQL DBMS๋ค์ ํ ์ด๋ธ๊ณผ ๊ฐ์ ์คํค๋ง๊ฐ ๊ณ ์ ๋ ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ์ง ์์
- ๋์ JSON ํํ์ ๋์ ์คํค๋งํ Document๋ฅผ ์ฌ์ฉํ๊ณ , ์ด๋ฅผ BSON์ด๋ผ๊ณ ์ง์นญ
- ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํฐ๋ฅผ Document(RDBMS์ row)๋ผ๊ณ ํ๊ณ , ์ด๋ค์ ์งํฉ์ Collection(RDBMS์ Table)์ด๋ผ ํจ
[ Robo3T ]
- ๊ณต์ ํํ์ด์ง : https://robomongo.org/
- Robo 3T๋ MongoDB ํธ์คํ ๋ฐฐํฌ๋ฅผ ์ํด ์ฌ์ฉ๋๋ ๊ทธ๋ํฝ ์ฌ์ฉ์ ์ธํฐํ์ด์ค์(GUI)
- ํ ์คํธ ๊ธฐ๋ฐ ์ธํฐํ์ด์ค ๋์ ์๊ฐ ํ์๊ธฐ๋ฅผ ํตํด ๋ฐ์ดํฐ์ ์ํธ ์์ฉ ๊ฐ๋ฅ
- ์ฆ MongoDB๋ฅผ ํตํด DB๋ฅผ ์ ์ฅํ ๋ ์ด DB ๋ด๋ถ๋ฅผ ์๊ฐ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๋ ํ๋ก๊ทธ๋จ
[ Requests ๋ผ์ด๋ธ๋ฌ๋ฆฌ ]
- ๊ณต์ ํํ์ด์ง : https://requests.readthedocs.io/en/latest/
- Requests๋ HTTP ์์ฒญ์ ๊ฐ๋จํ๊ฒ ํ๊ธฐ ์ํ ์ํ์น ๋ผ์ด์ผ์ค 2.0 python์ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
- ์์ฌ์ด HTTP/1.1 ์์ฒญ์ ์ฌ์ฉ ๊ฐ๋ฅ, URL์ ์ฟผ๋ฆฌ ๋ฌธ์์ด ์๋ ์ถ๊ฐ ๋ฐ POST ๋ฐ์ดํฐ ํผ ์ธ์ฝ๋ฉ ํ์x
# ๊ธฐ๋ณธ ์์
import requests
# ์ฌ์ดํธ์ ์ ์ ์ GET ํค๋๋ฅผ ์๋ฒ์ ์ ์กํ์ง๋ง ํ์ด์ฌ์ ํตํด HTTP ํต์ ์ ํ ๊ฒฝ์ฐ
# ๋ค์์ ํค๋ ์ ๋ณด๊ฐ ๋๋ฝ๋ ์ ์๊ธฐ์ ํ์์ ํค๋ ์๋ ์ง์ ๊ฐ๋ฅ
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'}
response = requests.get('https://www.naver.com/') # ํด๋น URL์ GET ๋ฐฉ์์ HTTP ์์ฒญ
print(response.status_code) # status_code == 200์ผ ๊ฒฝ์ฐ ์ ์์ ์ผ๋ก ์์ฒญ์ด ๋ ๊ฒ
print(response.encoding) # ์์ฒญํ URL์ encoding ํ์ ํ์ธ
print(response.text) # ์์ฒญํ URL์ HTML ์์ค ์ฝ๋ ์ ๋ถ๋ฅผ ์ถ๋ ฅ
[ BeautifulSoup ๋ผ์ด๋ธ๋ฌ๋ฆฌ ]
- ๊ณต์ ํํ์ด์ง : https://www.crummy.com/software/BeautifulSoup/bs4/doc/
- HTML ๋ฐ XML ํ์ผ์์ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๊ธฐ ์ํ python ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ์ ํธํ๋ parser์ ํจ๊ป ์๋ํ์ฌ ๊ตฌ๋ฌธ ๋ถ์ ํธ๋ฆฌ๋ฅผ ํ์, ๊ฒ์ ๋ฐ ์์ ํ ์ ์๋ ๊ด์ฉ์ ๋ฐฉ๋ฒ์ ์ ๊ณต
- ๋ณดํต request ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ฐ์ HTTP ์ฐ๊ฒฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํจ๊ป ์ฌ์ฉ๋จ
- ์ฐธ๊ณ : 2022.04.22 - [ํ๋ก๊ทธ๋๋ฐ ์ธ์ด/Python] - [Python] ๋ฐ์ดํฐ ๋ถ์ ๋ฐ ํ์ฉ (3)
# ๊ธฐ๋ณธ ์์
from bs4 import BeautifulSoup
# ํด๋น html ์์ค ์ฝ๋ ๊ฐ์ฒด๋ฅผ html ๋ฌธ๋ฒ๊ท์น์ ๋ฐ๋ฅธ ๋ฌธ์์ด์ธ html parser๋ก ๋ณํ
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify()) # html parser๋ก ๋ณํ๋ soup๋ฅผ ์๋ณํ๊ธฐ ์ฉ์ดํ ํํ๋ก ์ถ๋ ฅ
[ pymongo ๋ผ์ด๋ธ๋ฌ๋ฆฌ ]
- ๊ณต์ ํํ์ด์ง : https://pymongo.readthedocs.io/en/stable/
- ํ์ด์ฌ์์ MongoDB์ ์ฐ๋ํ์ฌ ์ฌ์ฉ์ ํ์ํ tool๋ค์ ์ ๊ณตํ๋ python ๋ผ์ด๋ธ๋ฌ๋ฆฌ
๐ Making a Connection with MongoClient
from pymongo import MongoClient
# ์ ์ํ ํด๋ผ์ด์ธํธ ์ฃผ์ ์ง์
client = MongoClient('localhost', 27017) # mongoDB๋ 27017 ํฌํธ๋ก ๋์๊ฐ
# client = MongoClient('mongodb://localhost:27017/')
๐ Getting a Database
# ํด๋น ํด๋ผ์ด์ธํธ์ DB์ ์ ๊ทผ (ํด๋น DB๊ฐ ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ์๋ ์์ฑ๋จ)
db = client.moviesDB # 'moviesDB' ๋ช
์ ๊ฐ์ง DB์ ์ ๊ทผ / ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ์์ฑ
# db = client['moviesDB']
๐ Getting a Collection
# DB์ Collection์ ์ ๊ทผ
collection = db.kor_movies # moviesDB > kor_movies ์ฝ๋ ์
์ ์ ๊ทผ
# collection = db['kor_movies']
๐ Documents
# ๋ชฝ๊ณ DB์ ๋ฐ์ดํฐ๋ JSON ์คํ์ผ์ ๋ฌธ์๋ฅผ ์ฌ์ฉํ์ฌ ํ์(์ ์ฅ)
movie_data = { 'title': '๋ฒ์ฃ๋์2',
'genre': '๋ฒ์ฃ, ์ก์
',
'nation': 'ํ๊ตญ',
'director': '์ด์์ฉ',
'release': '2022-05-18',
'star_score': 9.07 }
๐ Inserting a Document
# ๋ฐ์ดํฐ ์ฝ์
collection.insert_one(movie_data) # ํน์ํค์ธ "_id"ํค๊ฐ ์์ ๊ฒฝ์ฐ ์๋์ผ๋ก ์ถ๊ฐ๋จ

๐ Getting a Document
# ๋ฐ์ดํฐ ํ์
# find() : ์กฐ๊ฑด์ ๋ถํฉํ๋ ๋ชจ๋ ๋ฐ์ดํฐ ์ถ์ถ
# find_one() : ์กฐ๊ฑด์ ๋ถํฉํ๋ ํ๊ฐ์ง ๋ฐ์ดํฐ๋ง ์ถ์ถ
# {'_id': False}๋ฅผ ํด์ฃผ์ง ์์ผ๋ฉด ํด๋น ๋ฐ์ดํฐ์ _id ๊ฐ์ธ ObjectId๊น์ง ๋ถ๋ฌ์ด
print(collection.find_one({'title': '๋ฒ์ฃ๋์2'}, {'_id': False})) # 'title'์ด '๋ฒ์ฃ๋์2'์ธ ๋ฌธ์ ์ถ๋ ฅ
# ํน์ ์ฝ๋ ์
์ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ํ์ธํ ๊ฒฝ์ฐ ๋น ์ค๊ดํธ ์ด์ฉ
# print(list(db.users.find({}, {'_id': False})))

๐ Update a Document
# ๋ฐ์ดํฐ ์์
# update_many() : ํด๋น ์กฐ๊ฑด์ ๋ถํฉํ๋ ๋ฌธ์ ๋ชจ๋์ ์์ ์ฌํญ์ ์ํ
# update_one() : ํด๋น ์กฐ๊ฑด์ ๋ถํฉํ๋ ๋ฌธ์ ํ๊ฐ์ ์์ ์ฌํญ์ ์ํ
# 'title'์ด '๋ฒ์ฃ๋์2'์ธ ๋ฌธ์์ 'Leading actor' ์ value๋ฅผ '๋ง๋์'์ผ๋ก ๋ณ๊ฒฝ / ์์์ ์์ฑ ํ ์ฝ์
collection.update_one({'title': '๋ฒ์ฃ๋์2'}, {'$set': {'Leading actor': '๋ง๋์'}})
collection.update_one({'title': '๋ฒ์ฃ๋์2'}, {'$set': {'Leading actor': '์์๊ตฌ'}})


๐ Delete a Document
# ๋ฐ์ดํฐ ์ญ์
# delete_one() : ํด๋น ์กฐ๊ฑด์ ๋ถํฉ ํ๋ ๋ฌธ์ ํ๊ฐ ์ญ์
collection.delete_one({'Leading actor': '์์๊ตฌ'})

[ ์น์คํฌ๋ํ(ํฌ๋กค๋ง) ์์ ]
โ 2020.03.03 ๊ธฐ์ค ๋ค์ด๋ฒ ๋ญํน๋ณ(ํ์ ์/๋ชจ๋ ์ํ) ์ํ ์ ๋ณด ์ถ๋ ฅ
import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
# -- requests settings --
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=20200303',headers=headers)
# -- pymongo settings --
client = MongoClient('localhost', 27017)
db = client.dbsparta
# -- bs4 settings --
soup = BeautifulSoup(data.text, 'html.parser')
trs = soup.select('#old_content > table > tbody > tr')
for tr in trs:
a_tag = tr.select_one('.title > .tit5 > a')
if a_tag is not None:
# ac ํด๋์ค๊ฐ ๋๊ฐ ์กด์ฌํจ nth-child ๋ฌธ์ ํตํด ์๋ณ
movie_rating = tr.select_one('td:nth-child(1) > img')['alt']
movie_score = tr.select_one('td.point').text
movie_title = a_tag.text
print("[" + movie_rating + "] " + movie_title + " โ
: " + movie_score)
# doc = {'title': movie_title,
# 'rating': movie_rating,
# 'score': movie_score}
# db.movies_02.insert_one(doc)
โก 2020.04.03 ๊ธฐ์ค ์ง๋๋ฎค์ง 1-50์ ์ฐจํธ ํฌ๋กค๋ง ์์
import requests
from bs4 import BeautifulSoup
# -- requests settings --
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://www.genie.co.kr/chart/top200?ditc=D&ymd=20200403&hh=23&rtm=N&pg=1',headers=headers)
# -- bs4 settings --
soup = BeautifulSoup(data.text, 'html.parser')
trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
for tr in trs:
rating = list(tr.select_one('td.number'))[0].text.strip()
info = tr.select_one('td.info')
title = info.select_one('a.title').text.strip()
artist = info.select_one('a.artist').text
print(rating + " " + title + " " + artist)