网易云音乐爬虫python的代码
时间: 2024-08-16 07:06:04 浏览: 141
创建网易云音乐爬虫的Python代码通常涉及到使用第三方库如`requests`、`BeautifulSoup`或`selenium`来获取网页内容,因为网易云音乐的部分数据可能是通过JavaScript动态加载的。下面是一个简单的示例,展示如何使用`requests`和`BeautifulSoup`从歌单页面抓取歌曲名称:
```python
import requests
from bs4 import BeautifulSoup
def get_song_names(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'lxml')
song_list = []
for li in soup.find_all('li', class_='m-song-item'):
song_name = li.h2.a['title']
song_list.append(song_name)
return song_list
# 使用示例
url = "https://music.163.com/#/playlist/XXXXX" # 替换为实际的歌单ID
song_names = get_song_names(url)
for i, name in enumerate(song_names, start=1):
print(f'{i}. {name}')
阅读全文