python3的代码:
[Python] 纯文本查看 复制代码 # -*-coding:utf-8-*-
import re
import urllib
import requests
def downmp3(url1):
try:
print("正在解析网页,请稍后。\n")
pre_html = requests.get(url1).content.decode('utf-8')
except:
print("请检查网络连接是否正确。\n")
else:
print("解析完成")
key = re.findall(r'<div class="song-opera clearfix" data-songid=\'(.*?)\'>', pre_html, re.M | re.S)
url = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.play&songid=" + key[0]
html = requests.get(url).content.decode('utf-8')
dowbload_url = re.findall(r'"show_link":"(.*?)","free"', html, re.M | re.S)
mp3detail = re.findall(r'"lrclink":"(.*?)","', html, re.M | re.S)
download = dowbload_url[0].replace('\\', '')
lrc_temp = mp3detail[0].replace('\\', '')
mp3 = urllib.request.urlopen(download)
try:
print("正在下载歌曲,请稍后...\n")
output = open(key[0] + ".mp3", 'wb')
output.write(mp3.read())
output.close()
except IOError:
print("下载失败,请确认程序是否有写入权限!!!")
else:
print("下载歌曲成功")
lrc = requests.get(lrc_temp).content.decode('utf-8')
try:
print("正在下载歌词,请稍后...\n")
output = open(key[0] + ".lrc", 'w')
output.write(lrc)
output.close()
except IOError:
print("下载失败,请确认程序是否有写入权限!!!")
else:
print("下载歌词成功")
print("请输入歌名或歌手")
searchmp3 = input('')
searchmp3_respons = requests.get("http://music.baidu.com/search?key=" + searchmp3).content
print(searchmp3_respons.decode('utf-8'))
ff = open("temp.tmp", 'w')
ff.write(searchmp3_respons.decode('utf-8'))
ff.close()
ff = open("temp.tmp", 'r')
sstr = ff.read()
ff.close()
searchmp3_respons1 = searchmp3_respons
tag_songid = re.findall(r'data-songdata=\'{ "id": "(.*?)" }\'', sstr, re.M | re.S)
tag_div = re.findall(
r'data-film.*?data-info=.*?\'>(.*?)</a>.*?<span class="singer".*?<span class="author_list".*?href=".*?">(.*?)</a>',
sstr, re.M | re.S)
count = 1
for line in tag_div:
a = re.sub('<em>', '', line[0])
b = re.sub('</em>', '', a)
e = re.sub('\t', '', b)
f = re.sub('\n', '', e)
i = re.sub(' ', '', f)
c = re.sub('\t', '', line[1])
d = re.sub('\n', '', c)
g = re.sub('<em>', '', d)
h = re.sub('</em>', '', g)
j = re.sub(' ', '', h)
try:
print("[" + str(count) + "]\t\t" + i + "\t\t\t" + j)
except:
print("[" + str(count) + "]\t\t字体显示不出来,但是可以下载")
count = count + 1
print("请输入要下载歌曲的序号或者输入all全部下载")
input = input('')
if input == 'all':
for i in range(1, count):
str = "http://music.baidu.com/song/" + tag_songid[i - 1]
downmp3(str)
else:
urldownload = "http://music.baidu.com/song/" + tag_songid[int(input) - 1]
print(urldownload)
downmp3(urldownload)
print("\n下载完成,请按任意键结束...")
|