개발일기 21.05.16_파이썬

Nhahan·2021년 5월 16일
0

항해99 개발일기

목록 보기
12/31
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

0개의 댓글