코알라UNIV에서 5주차까지 배웠던 내용들을 모두 활용하여 수행할 수 있는 자체 제작 과제입니다.
추출할 데이터 세트
데이터 변수명 | 형식 | 비고 |
---|---|---|
rank | int | 순위 |
imgSrc | string | 이미지 링크 |
song | string | 노래 제목 |
artist | string | 아티스트명, 복수 가능 |
album | string | 앨범 제목 |
탭 이름 | 메뉴 입력 |
---|---|
실시간 | realtime |
급상승 | rise |
일간 | day |
주간 | week |
월간 | month |
menu = input("멜론차트 옵션을 입력하세요: ")
if menu == "realtime" :
raw = requests.get("https://www.melon.com/chart/index.htm", headers={"User-Agent": "Mozilla/5.0"})
else :
# 나머지 경우, url 주소의 규칙을 파악한 후 적절히 raw 변수를 설정해주시면 됩니다.
# .htm 다음 따라오는 params의 값은 모두 무시하셔도 상관 없습니다. .htm까지를 url로 넣어주세요!
sheet.append([int(rank), imgSrc, song, artistString, album])
로 저장하시면 됩니다!
try:
wb = openpyxl.load_workbook("MelonChartSheet.xlsx")
# 코드 작성
sheet.append(["순위", "앨범 사진 링크 주소", "곡 명", "아티스트 명", "앨범 명"])
print("불러오기 완료")
except:
wb = openpyxl.Workbook()
# 코드 작성
sheet.append(["순위", "앨범 사진 링크 주소", "곡 명", "아티스트 명", "앨범 명"])
print("새로 파일을 만들었습니다")
역시 구글링하면 간단하게 현재 시각 추출하는 법을 알 수 있습니다!
결과는 다음과 같습니다. 해당 폴더를 압축하시고 압축하신 폴더를 5월 28일 오후 2시까지 홈페이지에 제출해주세요!
month 메뉴에 대한 top 100 음악 앨범 커버 이미지입니다.
모든 과정을 다 수행하고 났을 때의 xlsx 결과물 모습입니다.
import requests
from bs4 import BeautifulSoup
import openpyxl
import datetime
from urllib.request import urlretrieve
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
########################################################################################################################
menu = input("멜론차트 옵션을 입력하세요: ")
now = datetime.datetime.now()
try:
wb = openpyxl.load_workbook("MelonChartSheet.xlsx")
sheetList = wb.sheetnames
for sheet in sheetList :
if menu == sheet :
wb.remove(wb[menu])
break
sheet = wb.create_sheet(menu)
sheet.append(["현재시각", now])
sheet.append(["순위", "앨범 사진 링크 주소", "곡 명", "아티스트 명", "앨범 명"])
print("불러오기 완료")
except:
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = menu
sheet.append(["현재시각", now])
sheet.append(["순위", "앨범 사진 링크 주소", "곡 명", "아티스트 명", "앨범 명"])
print("새로 파일을 만들었습니다")
########################################################################################################################
if menu == "realtime" :
raw = requests.get("https://www.melon.com/chart/index.htm", headers={"User-Agent": "Mozilla/5.0"})
else :
raw = requests.get("https://www.melon.com/chart/"+menu+"/index.htm", headers={"User-Agent": "Mozilla/5.0"})
html = BeautifulSoup(raw.text, 'html.parser')
########################################################################################################################
# 순위 : td > div.wrap.t_center > span.rank
# 앨범 사진 : div.wrap > a > img
# 곡명 : div.ellipsis.rank01 a
# 아티스트명 : div.ellipsis.rank02 > a:nth-of-type(num)
# 앨범명 : div.ellipsis.rank03 a
########################################################################################################################
for cnt in range(1, 3) :
if cnt == 1 :
container = html.select('tr#lst50')
else :
container = html.select('tr#lst100')
for c in container:
rank = c.select_one('td > div.wrap.t_center > span.rank').text.strip()
print(rank)
song = c.select_one('div.ellipsis.rank01 a').text.strip()
print(song)
artistNum = 1
artistString = ""
while True:
if c.select_one('div.ellipsis.rank02 > a:nth-of-type(' + str(artistNum) + ')') is not None:
artistString += (c.select_one('div.ellipsis.rank02 > a:nth-of-type(' + str(artistNum) + ')').text.strip() + '/')
else:
artistString = artistString[:-1]
print(artistString)
break
artistNum += 1
album = c.select_one('div.ellipsis.rank03 a').text.strip()
print(album)
img = c.select_one('div.wrap > a > img')
imgSrc = img.attrs["src"]
print(imgSrc)
urlretrieve(imgSrc, menu+'/'+rank+'_'+song[:2]+'.png')
sheet.append([int(rank), imgSrc, song, artistString, album])
print("="*20)
wb.save("MelonChartSheet.xlsx")