본문 바로가기
웹 기초/Crawling, MongoDB

DB 내용 조작하기, 지니 뮤직 Crawling

by 후닝훈 2021. 6. 28.
반응형

DB 조작

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

#insert
doc = {'name':'Dubo','age':21}
db.users.insert_one(doc)

#find
#id는 false로 지정해야 id값을 불러오지 않음 #60c617a 이런 값
same_ages = list(db.users.find({'age': 21},{'_id':False}))
print(same_ages)

same_ages = list(db.users.find({},{'_id':False}))

for p in same_ages:
    print(p)

#find_one
user = db.users.find_one({'name':'bobby'})
print(user)
print(user['age'])

#update
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})
db.users.update_many({'name':'bobby'},{'$set':{'age':19}})

#delete
db.users.delete_one({'name':'bobby'})

 

지니뮤직 Crawling

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://www.genie.co.kr/chart/top200?ditc=D&ymd=20200403&hh=23&rtm=N&pg=1',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')

trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')

for tr in trs:
    title = tr.select_one('td.info > a.title.ellipsis').text.strip()
    rank = tr.select_one('td.number').text[0:2].strip()
    artist = tr.select_one('td.info > a.artist.ellipsis').text
    print(rank, title, artist)
반응형

'웹 기초 > Crawling, MongoDB' 카테고리의 다른 글

Mongo DB 서버 종료  (0) 2021.07.06
Meta Tag Crawling  (0) 2021.07.04
크롤링 결과를 DB에 저장, Request 사용  (0) 2021.06.14
MongoDB Update, Delete  (0) 2021.06.13
Robo 3T & Mongo DB - Insert, Find  (0) 2021.06.13

댓글