[파이썬] 지니뮤직 스크롤

KBS·2021년 9월 8일
0

Python

목록 보기
2/4
post-thumbnail


지니뮤직에서 스크롤링을 하여 다음과 같이 나타내는 것이 목표이다.

우선 노래 제목을 받아와 출력해 보니 아래와 같이 공백이 생김.

공백은 검색해보니 .strip() 으로 깔끔하게 해결 가능했다.

굿!!

다음으로 랭킹을 받아오는데


아주 엉망이다.
원하는 숫자만 나타내 주려면 split을 이용해야할 것 같다.
.split(' ')[0]
를 붙여주도록 하자.


그 뒤에 가수 까지 붙여줘서 완성해줬다.

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')

trs = soup.select('#body-content > div.newest-list > div > table > tbody > tr ')

for tr in trs:
    a_tag= tr.select_one(' td.info > a.title.ellipsis')
    if a_tag is not None:
        rank = tr.select_one('td.number').text.split(' ')[0].strip()
        artist = tr.select_one('a.artist.ellipsis').text

        title = a_tag.text.strip()
        print(rank,title,artist)
profile
FE DEVELOPER

0개의 댓글