爬取酷狗音乐歌单python代码
时间: 2025-06-16 18:00:38 浏览: 15
### 使用 Python 编写爬虫程序以抓取酷狗音乐平台上的歌单数据
为了实现这一目标,可以采用 `requests` 和 `BeautifulSoup` 库来获取网页内容并解析 HTML 文档。以下是具体实现方法:
#### 导入所需库
```python
import requests
from bs4 import BeautifulSoup
```
#### 获取页面源码
定义函数用于发送 HTTP 请求,并返回响应对象的内容。
```python
def get_html(url):
try:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
r = requests.get(url, timeout=30, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except Exception as e:
print(f"Error occurred while fetching the page: {str(e)}")
return ""
```
#### 解析HTML文档
通过分析目标网站结构找到歌单列表所在的标签及其属性,进而提取出每首歌曲的信息。
```python
def parse_playlist(html):
soup = BeautifulSoup(html, 'html.parser')
playlist_items = []
for item in soup.select('.pc_temp_songlist .pc_temp_item'):
song_info = {}
# 提取歌曲名称
song_name_tag = item.find('span', class_='txt')
if song_name_tag is not None:
song_info['name'] = song_name_tag.a.string.strip()
# 提取歌手名
artist_tag = item.find('span', class_='singer')
if artist_tag is not None and artist_tag.span is not None:
song_info['artist'] = artist_tag.span.attrs.get('title')
# 添加到列表中
if all(key in song_info for key in ('name', 'artist')):
playlist_items.append(song_info)
return playlist_items
```
#### 主逻辑控制流程
指定要访问的目标URL以及输出路径等参数;调用上述两个辅助函数完成整个过程。
```python
if __name__ == "__main__":
url = "https://www.kugou.com/yy/html/rank.html" # 此处仅为示例链接,请替换为实际想要抓取的榜单页网址
html_content = get_html(url)[^1]
songs_list = parse_playlist(html_content)[^2]
for idx, song in enumerate(songs_list[:10], start=1): # 只打印前10条记录作为测试
print(f"{idx}. Song Name: {song['name']} | Artist: {song['artist']}")
```
此段代码展示了如何利用Python编写一个简单的网络爬虫脚本来收集来自酷狗音乐官网特定排行榜中的部分曲目详情[^3]。
阅读全文
相关推荐









