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

Python使用BeautifulSoup爬取Billboard热歌榜返回空列表无报错排查

问题根因
  • 日期输入格式错误:代码提示要求输入格式为YYYY-MM-DD,你输入的12-12-2012为DD-MM-YYYY格式,拼接后的URL不符合Billboard站点的路由规则,请求返回的不是对应日期的榜单页面,无对应目标元素。
  • 选择器过时失效:Billboard网站前端已迭代更新,你使用的旧类名chart-element__information__song早已不存在于当前页面结构中,无法匹配到歌曲名元素。
  • 反爬机制拦截:未设置请求头的User-Agent参数,默认的requests请求标识会被站点识别为爬虫,返回无榜单内容的反爬响应。
修复方案
  1. 修正日期输入,严格按照YYYY-MM-DD格式输入,比如查询2012年12月12日的榜单需输入2012-12-12
  2. 添加请求头伪装浏览器请求,避免被反爬拦截
  3. 替换为当前页面对应的歌曲名选择器

修正后代码示例:

from bs4 import BeautifulSoup
import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}

date = input("Which year do you want to travel to? Type the date in this format YYYY-MM-DD: ")
# 简单校验日期格式
date_parts = date.split("-")
if len(date_parts) != 3 or len(date_parts[0]) !=4:
    print("日期格式错误,请输入YYYY-MM-DD格式,例如2012-12-12")
else:
    response = requests.get("https://www.billboard.com/charts/hot-100/" + date, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    # 匹配当前页面歌曲名的h3标签
    song_elements = soup.select("li.o-chart-results-list__item h3.c-title")
    song_names = [song.get_text(strip=True) for song in song_elements]
    print(song_names)

内容的提问来源于stack exchange,提问作者Arunodai kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 10:54:04