3주차 과제(클로링)

신유빈·2021년 7월 26일

항해99

목록 보기
5/37
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:
    title = tr.select_one(' td.info > a.title.ellipsis').text.strip()
    nums = tr.select_one('td.number').text[0:2].strip()
    people = tr.select_one('td.info > a.artist.ellipsis').text
    print(nums, title, people )

지니에서 순위와 노래제목,가수를 클로링해서 가지고 오는 과제를 하였다.

일단 모두 포함되어 있는 큰틀을 가지고오고 거기서 하나하나 복사해서 가지고 오는 과제였다.

중간에 공백이 많아서 strip()로 공백을 제거해주었다.
<파이썬에서 공백을 제거할 때 사용하는 함수>
strip : 인자로 전달된 문자를 String의 왼쪽과 오른쪽에서 제거합니다.
lstrip : 인자로 전달된 문자를 String의 왼쪽에서 제거합니다.
rstrip: 인자로 전달된 문자를 String의 오른쪽에서 제거합니다.

profile
안녕하세요

0개의 댓글