写一个爬取网易云音乐歌单信息包含了歌单的ID、名字、播放次数、创建时间、创建歌单的用户、歌单的类型、更新时间、收藏数量、歌曲数量、歌单的描述、歌单的评论数量、分享数量的代码
时间: 2023-07-10 10:30:46 浏览: 341
好的,以下是爬取网易云音乐歌单信息的 Python 代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://music.163.com/discover/playlist'
headers = {
'Referer': 'https://music.163.com/',
'Host': 'music.163.com',
'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'
}
# 获取歌单页面HTML
def get_page(url):
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
return soup
# 爬取歌单信息
def get_playlists(soup):
playlists = []
for li in soup.find('ul', {'class': 'm-cvrlst f-cb'}).find_all('li'):
playlist = {}
playlist['id'] = li.find('a')['href'].split('=')[1]
playlist['name'] = li.find('a')['title']
playlist['play_count'] = li.find('span', {'class': 'nb'}).text
playlist['create_time'] = li.find('span', {'class': 's-fc3'}).text
playlist['create_user'] = li.find('a', {'class': 'nm nm-icn f-thide s-fc3'})['title']
playlist['type'] = li.find('span', {'class': 's-fc4'}).text
playlist['update_time'] = li.find('span', {'class': 's-fc4'}).text
playlist['collect_count'] = li.find('span', {'class': 'u-btni u-btni-fav '}).text
playlist['song_count'] = li.find('span', {'class': 's-fc3'}).text
playlist['description'] = li.find('div', {'class': 'u-cover u-cover-1'}).text
playlist['comment_count'] = li.find('span', {'class': 'u-btni u-btni-cmmt '}).text
playlist['share_count'] = li.find('span', {'class': 'u-btni u-btni-share '}).text
playlists.append(playlist)
return playlists
# 打印歌单信息
def print_playlists(playlists):
for playlist in playlists:
print('歌单ID:', playlist['id'])
print('歌单名字:', playlist['name'])
print('播放次数:', playlist['play_count'])
print('创建时间:', playlist['create_time'])
print('创建歌单的用户:', playlist['create_user'])
print('歌单类型:', playlist['type'])
print('更新时间:', playlist['update_time'])
print('收藏数量:', playlist['collect_count'])
print('歌曲数量:', playlist['song_count'])
print('歌单描述:', playlist['description'])
print('歌单评论数量:', playlist['comment_count'])
print('歌单分享数量:', playlist['share_count'])
print('----------------------------------')
# 程序入口
if __name__ == '__main__':
soup = get_page(url)
playlists = get_playlists(soup)
print_playlists(playlists)
```
该程序爬取网易云音乐的歌单页面,获取每个歌单的ID、名字、播放次数、创建时间、创建歌单的用户、歌单的类型、更新时间、收藏数量、歌曲数量、歌单的描述、歌单的评论数量、分享数量,并打印输出到控制台。注意,由于网易云音乐有反爬虫机制,程序中使用了一些请求头和BeautifulSoup库来模拟浏览器请求。
阅读全文