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://www.genie.co.kr/chart/top200?ditc=D&ymd=20200403&hh=23&rtm=N&pg=1',headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
for i in range(len(soup.select('#body-content > div.newest-list > div > table > tbody > tr'))):
title = soup.select_one("#body-content > div.newest-list > div > table > tbody > tr:nth-child(" + str(i) + ") > td.info > a.title.ellipsis")
rank = soup.select_one("#body-content > div.newest-list > div > table > tbody > tr:nth-child(" + str(i) + ") > td.number")
artist = soup.select_one("#body-content > div.newest-list > div > table > tbody > tr:nth-child(" + str(i) + ") > td.info > a.artist.ellipsis")
print(title.text.strip(),rank.text.split("\n")[0],artist.text) if title != None else None