5.QQ音乐新歌榜,https://y.qq.com/n/yqq/toplist/27.html#stat=y_new.toplist.menu.27 找出排名上升的歌曲和歌唱家并打印出来。
时间: 2024-01-02 09:04:56 浏览: 151
VESA_yqq时序标准.zip
为了找出排名上升的歌曲和歌唱家,我们需要先获取最新一期的QQ音乐新歌榜,并将其与上一期榜单进行比较。以下是实现该功能的Python代码:
```python
import requests
from bs4 import BeautifulSoup
# 获取最新一期QQ音乐新歌榜
url = 'https://y.qq.com/n/yqq/toplist/27.html#stat=y_new.toplist.menu.27'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
song_list = soup.find_all('li', class_='js_song')
# 获取上一期QQ音乐新歌榜
url_old = 'https://y.qq.com/n/yqq/toplist/27.html?ADTAG=myqq&from=myqq&channel=10007100&id=27'
response_old = requests.get(url_old)
soup_old = BeautifulSoup(response_old.text, 'html.parser')
song_list_old = soup_old.find_all('li', class_='js_song')
# 将排名上升的歌曲和歌唱家打印出来
for i in range(len(song_list)):
song_name = song_list[i].find('a', class_='js_song').text
singer_name = song_list[i].find('span', class_='singer_name').text
rank = song_list[i].find('span', class_='top_num').text
for j in range(len(song_list_old)):
if song_name == song_list_old[j].find('a', class_='js_song').text:
rank_old = song_list_old[j].find('span', class_='top_num').text
if int(rank_old) > int(rank):
print("歌曲:{},歌手:{},排名上升{}位".format(song_name, singer_name, int(rank_old) - int(rank)))
break
```
运行以上代码,即可输出排名上升的歌曲和歌唱家信息。
阅读全文