Python爬取QQ音乐排行榜的代码
时间: 2023-12-25 17:23:01 浏览: 324
爬虫-爬取音乐排行榜音乐.rar
好的,这个问题可以回答。以下是Python爬取QQ音乐排行榜的代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求,获取HTML页面内容
url = "https://y.qq.com/n/yqq/toplist/4.html"
response = requests.get(url)
html = response.text
# 用BeautifulSoup解析页面内容
soup = BeautifulSoup(html, "html.parser")
song_list = soup.find_all("li", class_="js_song")
# 遍历歌曲信息,提取歌曲名字和演唱者
for song in song_list:
song_name = song.find("div", class_="songname").text.strip()
singer = song.find("div", class_="singer").text.strip()
print(song_name, "-", singer)
```
希望能帮到你!
阅读全文