You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Kworb抓取排名、歌手及歌曲信息并导出为Excel?

提取Kworb Spotify美国周榜数据并保存为Excel

问题说明

现有脚本仅能输出页面全部文本,无法单独提取**排名(Pos)、歌手(Artist)、歌曲(Songs)**信息,需修改脚本实现精准提取并保存为Excel文件。

前置准备

先安装所需依赖包:

pip install requests beautifulsoup4 pandas openpyxl

完整实现脚本

import requests
from bs4 import BeautifulSoup
import pandas as pd

# 发送请求获取页面内容
url = "https://kworb.net/spotify/country/us_weekly.html"
response = requests.get(url)
response.encoding = "utf-8"
soup = BeautifulSoup(response.text, 'html.parser')

# 定位目标表格(Kworb榜单数据在class为stats的表格中)
table = soup.find('table', class_='stats')
rows = table.find_all('tr')[1:]  # 跳过表头行

# 初始化存储数据的列表
data = []
for row in rows:
    cols = row.find_all('td')
    # 提取排名、歌手、歌曲信息
    pos = cols[0].get_text(strip=True)
    # 歌曲名在第二个td的a标签内
    song = cols[1].find('a').get_text(strip=True)
    # 歌手名在第三个td的a标签内
    artist = cols[2].find('a').get_text(strip=True)
    data.append([pos, artist, song])

# 转换为DataFrame并保存为Excel
df = pd.DataFrame(data, columns=['Pos', 'Artist', 'Songs'])
df.to_excel('spotify_us_weekly_chart.xlsx', index=False, engine='openpyxl')
print("数据已成功保存为spotify_us_weekly_chart.xlsx")

脚本说明

  • 定位表格:通过class='stats'定位榜单所在表格,跳过第一行表头
  • 提取数据:遍历每一行,从对应<td>标签的<a>元素中提取歌曲和歌手名,直接提取排名文本
  • 保存Excel:用pandas将数据转为结构化的DataFrame,保存时关闭默认索引列,生成标准格式的Excel文件

内容的提问来源于stack exchange,提问作者Hi Hi try

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:03:36