用Python写一个免费抓取网易云音乐热歌的爬虫
时间: 2024-05-18 18:15:39 浏览: 153
好的,下面是一个用Python写的免费抓取网易云音乐热歌的爬虫,你可以参考一下:
```
import requests
from bs4 import BeautifulSoup
url = 'http://music.163.com/discover/toplist?id=3778678'
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, 'html.parser')
song_list = soup.find('ul', {'class': 'f-hide'}).find_all('a')
for song in song_list:
print(song.text)
```
这个爬虫使用了Requests库和BeautifulSoup库。首先,我们发送一个GET请求获取网页的HTML代码,然后使用BeautifulSoup库对HTML代码进行解析,提取出热歌榜中的歌曲信息,并输出歌曲名称。注意,这个爬虫只能获取前100首热歌,如果要获取更多的歌曲,需要对网页进行翻页处理。
阅读全文
相关推荐













