728x90
개요
- selenium 라이브러리의 webdriver을 활용해 웹 브라우저를 조작하고,
- BeautifulSoup 라이브러리를 활용해 웹 페이지 상의 HTML 데이터에서 필요한 정보를 가져올 수 있음.
라이브러리 및 기타 준비
# selenium 설치
!pip install selenium
# selenium 라이브러리 불러오기
from selenium import webdriver
# 크롬드라이버 준비 - 크롬 드라이버는 별도 다운로드 필요 아래 더보기에 다운로드 링크 첨부
driver = webdriver.Chrome('./chromedriver.exe')
# BeautifulSoup 라이브러리 불러오기
from bs4 import BeautifulSoup
더보기
Downloads - ChromeDriver - WebDriver for Chrome (google.com)
* 크롬 드라이버는 selenium의 webdriver를 통해 파이썬에서 크롬 브라우저를 제어할 수 있게 해줌.
* 본인의 크롬 버전을 확인하고 맞는 버전을 설치해 줄 것.
멜론 노래 순위 정보 크롤링
# 멜론 실시간 top100 차트
url = 'http://www.melon.com/chart/index.htm'
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
#####################################################
songs = soup.select('table > tbody > tr')
print("전체 노래 곡 수 : ", len(songs))
print("현재 1위인 노래 :\n", songs[0])
song = songs[0]
# 1위 곡 제목 불러오기
title = song.select('div.ellipsis.rank01 > span > a')[0].text
print("곡 이름 : ", title)
# 1위 곡 가수 불러오기
singer = song.select('div.ellipsis.rank02 > a')[0].text
print("가수 명 : ", singer)
# 1위부터 100위까지 곡 제목과 가수 불러오기
rank = 1
for song in songs:
title = song.select('div.ellipsis.rank01 > span > a')[0].text
singer = song.select('div.ellipsis.rank02 > a')[0].text
print(f"{rank}위 : ", title, " - ", singer)
rank += 1
'Python' 카테고리의 다른 글
PySpark MLlib (0) | 2022.06.17 |
---|---|
Python GUI module - tkinter (0) | 2021.09.11 |