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

爬取虾米音乐热歌榜

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

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

全部评论

相关推荐

11-29 00:55
门头沟学院
区域赛银,邀请赛金,打算十二月打下Java基础、背点八股、写个外卖后去投福建小厂的寒假实习,简历应该怎么写呢?以及福州/和厦门有推荐的小厂吗?
牛客53210502...:简历一页:把区域银,邀请赛金标粗,其他的奖除非凑一页否则没有必要写。或者多页:每个站一行这样都列出来。项目经历看看牛客其他人是怎么写的,写的不好呢。简历打磨好按部就班没问题的
点赞 评论 收藏
分享
11-11 16:40
已编辑
门头沟学院 人工智能
不知道怎么取名字_:这个有点不合理了,相当于已经毕业了,但还是没转正,这不就是白嫖
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务