개발 => 복습 후 재정리 대기/Python

[Python][bs4] 지니뮤직 크롤링 실습

장 상 현 2021. 5. 9.

 

import requests
from bs4 import 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=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:
    rank = tr.select_one('td.number').text[0:2].strip()
    title = tr.select_one('td.info > a.title.ellipsis').text.strip()
    artist = tr.select_one('td.info > a.artist.ellipsis').text

    print(rank, title, artist)

.text 로 문자만

 

.strip() 으로 공백제거

 

[x:y] 로 문자열 자르기

'개발 => 복습 후 재정리 대기 > Python' 카테고리의 다른 글

[Python] Stack, 스택  (0) 2021.05.28
[Flask][Python] 기본 틀  (0) 2021.05.09
[Python][문법] class, instance  (0) 2021.05.06
[Python][문법] 함수 인자  (0) 2021.05.06
[Python][문법] map, lambda, filter  (0) 2021.05.06

댓글