지니뮤직에서 랭킹 제목 가수 이름 퍼오기 파이썬

Soonsoo Book Club·2021년 8월 30일
post-thumbnail

랭킹 제목 가수

# 라이브러리에서 requests, bs4 import
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'}

# 데이터를 링크 주소에서 html로 받아오고 soup에 html을 가져온다.
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')

# html 데이터들 중에서 특정 데이터들을 선택해서 trs에 담는다.
trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr')

# 가져온 trs을 데이터 별로 각 변수에 담는다.
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)코드를 입력하세요

0개의 댓글