본문 바로가기

frontend

3주차 숙제 : 지니뮤직 크롤링하기

import requests # import하는거
from bs4 import BeautifulSoup
from pymongo import MongoClient #pymongo를 쓰겠습니다
client = MongoClient('localhost', 27017) # 내 컴퓨터에서 지금 돌아가고 있는 mongoDB에 접속할 겁니다.
db = client.dbsparta   # dbsparta라고 하는 DB 이름으로 접속할 겁니다. 없으면 자동으로 생성


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.number
#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.info > a.artist.ellipsis

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)

'frontend' 카테고리의 다른 글

2022-04-12  (0) 2022.04.12
2022-04-11  (0) 2022.04.12
3주차 개발일지(Weak I learned)  (0) 2022.04.12
2022-04-10  (0) 2022.04.12
2022-04-09  (0) 2022.04.12