공부하자/웹개발일지

[웹개발일지] #7. 지니뮤직 크롤링하기

snbrin 2022. 3. 8. 15:22

 

 

 

💻세번째 과제물💻

👉지니뮤직 사이트 크롤링해서 음원 순위 가져오기!!

 

순위, 제목, 가수 순서대로

추출 정보가 깔끔하게 보이지 않을 경우,

파이썬 내장 함수인 strip()를 활용하기

 

https://github.com/janghr1225/pythonprac

 

GitHub - janghr1225/pythonprac: 파이썬 연습(지니뮤직 크롤링, 퀴즈)

파이썬 연습(지니뮤직 크롤링, 퀴즈). Contribute to janghr1225/pythonprac development by creating an account on GitHub.

github.com

 


 

💻완성💻

 

 

여기서 상기해야할 것은?

👉 파이몽고 기본 코드

👉 bs4 기본 코드

👉 글자 배열 안맞는 것은 strip()함수

text.strip( ) 사용

import requests
from bs4 import BeautifulSoup


#파이몽고 쓸게염
from pymongo import MongoClient
#내 로컬에서 사용할게염
client = MongoClient('localhost', 27017)
#dbsparta라는 db이름으로 접속할거에염
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&rtm=N',headers=headers)

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

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

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.strip()
    print(rank, title, artist)