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

爬取虾米音乐热歌榜

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

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文件,但是不知道虾米的下载路径是什么,无奈就爬了文本了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arik (IoT)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值