天天看點

基于python爬取網易雲熱榜并下載下傳.

大一國小期作業,發網上存檔.

import requests
import datetime
from lxml import etree
import easygui as eg
songid=[]
songname=[]
url='https://music.163.com/discover/toplist?id=3778678'
head={
     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=html.xpath('//a[contains(@href,"song?")]')
id_list=id_list[0:-11]
today=datetime.date.today()
eg.msgbox('擷取網易雲音樂熱門歌曲,今日是 %s' % (today),'擷取網易雲音樂熱門歌曲')
for id in id_list:
     href=id.xpath('./@href')[0]
     song_id=href.split('=')[1]
     songid.append(song_id)
     song_name=id.xpath('./text()')[0]
     songname.append(song_name)
temp=dict(zip(songname,songid))
select=eg.choicebox('選擇你要下載下傳的歌','下載下傳',songname)
down_url='https://music.163.com/song/media/outer/url?id='+str(temp[select])
music=requests.get(url=down_url,headers=head).content
with open(select+'.mp3','wb')as f:
    f.write(music)
eg.msgbox('下載下傳完成')