
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
안녕하세요. 현재 웹 개발 3주차 숙제를 진행중입니다.
웹크롤링을 해와서 rank, title, artist에 다 잘 넣었습니다.
그런데 title을 불러오던 중,
title.text를 하니까 제목 뿐만 아닌 span태그 내부에 있는 '19금'도 딸려서 나옵니다.
그래서 계속 틀어지는데 <a>태그 내부에 있는 span태그를 어떻게 제거해야 할까요?


import requests
from bs4 import BeautifulSoup
# 몽고 DB 사용
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.aef98yx.mongodb.net/Cluster0?retryWrites=true&w=majority')
db = client.dbsparta
# Request, beautifulSoup 사용
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=M&rtm=N&ymd=20210701', headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
# rank, title, artist
# rank
# 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(2) > td.number
# title
# 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(2) > td.info > a.title.ellipsis
# artist
# body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis
# body-content > div.newest-list > div > table > tbody > tr:nth-child(2) > td.info > a.title.ellipsis
#body-content > div.newest-list > div > table > tbody > tr:nth-child(1) > td.info > a.artist.ellipsis
musics = soup.select('#body-content > div.newest-list > div > table > tbody > tr')
for music in musics:
a = music.select_one('td.number')
if a is not None:
rank = a.text[0:2].strip()
title = music.select_one('td.info > a.title.ellipsis')
title_2 = title.text.strip()
# print(title_2)
artist = music.select_one(' td.info > a.artist.ellipsis')
artist_2 = artist.text
print(rank,title_2,artist_2)
