爬取网易云歌单并存入表格
时间: 2024-03-19 18:07:15 浏览: 187
好的,我可以帮你实现这个功能。不过,在开始之前,我需要确认一下你安装了以下的Python库:
- requests
- beautifulsoup4
- pandas
如果你没有安装的话,可以通过以下命令来安装:
```
pip install requests beautifulsoup4 pandas
```
现在,我们可以开始编写代码了。首先,我们需要使用requests库获取网易云音乐歌单页面的HTML内容。代码如下:
```python
import requests
url = 'https://music.163.com/playlist?id=710082302' # 这里的id是歌单的id,可以根据自己需要修改
headers = {
'Referer': 'https://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'
}
response = requests.get(url, headers=headers)
html = response.text
```
接下来,我们使用beautifulsoup4库来解析HTML内容,获取歌单中的歌曲信息。代码如下:
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
songs = soup.find('ul', {'class': 'f-hide'}).find_all('a')
song_list = []
for song in songs:
song_name = song.text
song_id = song['href'].split('=')[-1]
song_url = 'https://music.163.com/song/media/outer/url?id=' + song_id + '.mp3'
song_list.append({'name': song_name, 'url': song_url})
```
最后,我们将歌曲信息存储到表格中,使用pandas库来实现。代码如下:
```python
import pandas as pd
df = pd.DataFrame(song_list)
df.to_excel('song_list.xlsx', index=False)
```
这样,就完成了网易云音乐歌单爬取并存入表格的功能。完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://music.163.com/playlist?id=710082302' # 这里的id是歌单的id,可以根据自己需要修改
headers = {
'Referer': 'https://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'
}
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'lxml')
songs = soup.find('ul', {'class': 'f-hide'}).find_all('a')
song_list = []
for song in songs:
song_name = song.text
song_id = song['href'].split('=')[-1]
song_url = 'https://music.163.com/song/media/outer/url?id=' + song_id + '.mp3'
song_list.append({'name': song_name, 'url': song_url})
df = pd.DataFrame(song_list)
df.to_excel('song_list.xlsx', index=False)
```
阅读全文