본문 바로가기

카테고리 없음

3주차 숙제

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=M&rtm=N&ymd=20210701',headers=headers)

soup = BeautifulSoup(data.text, 'html.parser')

# print(soup)

# 코딩 시작

#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.title.ellipsis 노래 제목
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.number 노래 순위
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis 가수
musics = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
# print(musics)

for music in musics:
    a = music.select_one('td.info > a.title.ellipsis')
    if a is not None:
        title = a.text.strip()
        rank = music.select_one('td.number').text[0:2].strip()
        artist = music.select_one('td.info > a.artist.ellipsis').text

    print(rank, title, artist)

지니뮤직 크롤링

 

크롤링 할 데이터 우클릭 > 검사 > console창에서 해당 영역 우클릭 후 copy > copy selector