본문 바로가기
웹 프로그래밍(풀스택-->python)

38. 끝, 숙제를 해보자

by 백엔드개발자0107 2021. 4. 25.

자, 이제 숙제를 해보자!

 

자, 이런식으로 숙제를 해볼것이다..

 

지니뮤직 사이트는 아래와 같이 생겼다.

 

 

기본코드 템플릿은

 

 

from bs4 import BeautifulSoup
import requests
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')

이거고 이 템플릿을 이용해보자!

 

from bs4 import BeautifulSoup
import requests
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.number
# body-content > div.newest-list > div > table > tbody > tr:nth-child(1)
# body-content > div.newest-list > div > table > tbody > tr:nth-child(2)
# body-content > div.newest-list > div > table > tbody > tr:nth-child(1) >
# 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.number

for tr in trs:
    title = tr.select_one("td.info > a.title.ellipsis")
    title_text = title.text
    title_textStrip = title_text.strip()
    rank = tr.select_one("td.number").text[0:2].strip()
    singer = tr.select_one("td.info > a.artist.ellipsis")
    singer_text = singer.text
    singer_textStrip = singer_text.strip()
    print(rank, title_textStrip, singer_textStrip)

 

위와 같이 내가 작성한 코드가 있다.

 

여기서 python의 strip() 함수는 공백을 제거해주는 함수라는 것을 알게되었다..