[웹 개발] 3주차: 숙제 지니뮤직 크롤링

최미혜·2022년 4월 6일
0

생각보다 구글링을 잘 못해서 시간이 오래 걸렸다.
코드를 조금씩 짧게 하는 거에 익숙해지고 있다.
아주 나이스-!

아래는 내 코드

import requests
from bs4 import BeautifulSoup

import re

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

#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info
for tr in trs:
    chart = tr.select_one('td.info > a')
    if chart is not None:
        ranks = tr.select_one('td.number').text
        ranking = re.findall(r'\d', ranks)[0]
        title = tr.select_one('td.info> a.title.ellipsis').text.strip()
        singer = tr.select_one('td.info> a.artist.ellipsis').text.strip()

        print(ranking, title, singer)

0개의 댓글