python爬取网易云音乐飙升榜音乐_python爬取网易云音乐热歌榜 python爬取网易云音乐热歌榜实例代码...
时间: 2023-07-21 16:56:16 浏览: 95
好的,以下是 Python 爬取网易云音乐热歌榜的实例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://music.163.com/discover/toplist?id=3778678'
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'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
song_list = soup.find('ul', {'class': 'f-hide'}).find_all('a')
for song in song_list:
print(song.text)
```
这个代码会爬取网易云音乐热歌榜上的歌曲名,并输出到控制台。你可以根据自己的需要进行修改。注意要添加请求头,否则会被网易云音乐拒绝访问。
相关问题
python爬取网易云音乐飙升榜音乐,网易云音乐-飙升榜歌曲信息爬取
好的,可以使用Python的requests库和BeautifulSoup库来爬取网易云音乐飙升榜的歌曲信息。以下是示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 请求头信息
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.36'}
# 爬取页面
url = 'https://music.163.com/discover/toplist?id=19723756'
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析页面
song_list = soup.find('ul', {'class': 'f-hide'}).find_all('a')
for song in song_list:
print(song.text)
```
这段代码会输出网易云音乐飙升榜的所有歌曲名称。你可以根据自己的需要进行修改和扩展。
python爬取音乐排行_python爬取网易云音乐热歌榜实例代码
以下是用 Python 爬取网易云音乐热歌榜的实例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://music.163.com/discover/toplist'
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)
soup = BeautifulSoup(response.text, 'html.parser')
song_list = soup.find_all('div', class_='ttc')
for song in song_list:
print(song.a.attrs['title'])
```
解析:
1. 使用 requests 模块请求网页源代码。
2. 使用 BeautifulSoup 模块解析网页源代码,并通过类名查找到歌曲列表。
3. 遍历歌曲列表,获取每首歌曲的名称并输出。