☑️ 3주차 마지막 숙제
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:
song = 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,song,artist)
✔︎ 이 부분이 막혀서 검색해서 알아보는 중
.text[0:2]가 어떤 기능을 하기에 정렬이 쫙 되는 건지 모르겠다...
나머지는 해결했는데 이 부분 막혀서 답지 참고함,,🥲
rank = tr.select_one('td.number').text[0:2].strip()
[:] 처음부터 끝까지
[start:] start오프셋부터 끝까지
[:end] 처음부터 end-1 오프셋까지
[start : end] start오프셋부터 end-1 오프셋까지
[start : end : step] step만큼 문자를 건너뛰면서, 위와 동일하게 추출