虾米音乐榜单(不是音乐)爬爬爬

爬取虾米音乐热歌榜

本来还想着么得素材来写,今天就出来了,给人写了个简单的爬虫,就发上来吧

import time
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
from selenium.webdriver.common.action_chains import ActionChains

url = "https://www.xiami.com/"
driver = webdriver.Chrome()
driver.get(url)
#进入排行榜
xrank_all = '//*[@id="app"]/div/div[2]/div[1]/div[1]/div/div/div[1]/a[2]'
rank_all = driver.find_element_by_xpath(xrank_all)
ActionChains(driver).click(rank_all).perform()
time.sleep(1)
#进入热歌榜
xrank_all = '//*[@id="app"]/div/div[2]/div[1]/div[1]/div[1]/div[1]/div/div/div[2]'
rank_all = driver.find_element_by_xpath(xrank_all)
ActionChains(driver).click(rank_all).perform()
time.sleep(1)
#进入详细//*
xrank_New = '//*[@id="app"]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[2]/a'
rank_New = driver.find_element_by_xpath(xrank_New)
ActionChains(driver).click(rank_New).perform()
time.sleep(5)
#获取榜单
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")

songs = soup.find_all("div", class_="song-name em")
singers = soup.find_all("div",class_="singers COMPACT")
albums = soup.find_all("div",class_="album")
times = soup.find_all('div',class_='duration-container ops-container')

song_name = []
singers_name = []
albums_name = []
time_list = []

for singer in singers:
    singers_name.append(singer.text)
for song in songs:
    song_name.append(song.text)
for album in albums:
    albums_name.append(album.text)
for time in times:
    time_list.append(time.text)

singers_name.pop(-1)

index = [i for i in range(1,101)]

data = {'排名':index,'歌曲名字':song_name,'歌手':singers_name,'专辑':albums_name,'时长':time_list}
dataframe = pd.DataFrame(data)
dataframe.to_csv('Xiami_hot.csv',index=False,sep=',',encoding='utf-8-sig')

driver.close()

本来想的是爬音乐的,就是内种MP3文件,但是不知道虾米的下载路径是什么,无奈就爬了文本了。

发布了32 篇原创文章 · 获赞 14 · 访问量 1201

猜你喜欢

转载自blog.csdn.net/qq_45770364/article/details/105063462