89028901 发表于 2021-1-26 19:39

Python抓取全网音乐

支持音乐平台(网易,QQ,酷狗,酷我)

描述:

1.下载音乐文件(.mp3)

2.下载歌词文件(.lrc)

注意:部分搜出来的歌,下载后无法播放的原因是,歌曲地址出现错误,不关代码问题,是网站!

代码写的菜,不是我的错,有大神可以帮我改进下吗?{:1_352:}

Github:https://github.com/AYueMortal/ParseMusic/

代码本体:import requests
import jsonpath

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"
}
print("——————————")
print("1-网易")
print("2-QQ")
print("3-酷狗")
print("4-酷我")
types = int(input("请选择搜索引擎:"))
if types == 1:
    type = "netease"
elif types == 2:
    type = "qq"
elif types == 3:
    type = "kugou"
elif types == 4:
    type = "kuwo"
else:
    print("输入有误,程序已退出!")
    exit()
keyword = str(input("请输入搜索歌曲:"))
data = {
    'input': keyword,
    'filter': 'name',
    'type': type,
    'page': '1'
}
url = "https://tool22.com/zb_tools/ajax.php?act=MusicTools"
url_html = requests.post(url, headers=headers, data=data).json()
titles = jsonpath.jsonpath(url_html, "$..title")
authors = jsonpath.jsonpath(url_html, "$..author")
urls = jsonpath.jsonpath(url_html, "$..url")
lrcs = jsonpath.jsonpath(url_html, "$..lrc")
pages =
print("——————————")
for title, author, page in zip(titles, authors, pages):
    print(f'{page}.{title} - {author}')

output = int(input("请输入下载的数字序号:"))
if output == 1:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors
elif output == 2:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors
elif output == 3:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors   
elif output == 4:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors   
elif output == 5:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors
elif output == 6:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors
elif output == 7:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors
elif output == 8:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors
elif output == 9:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors
elif output == 10:
    hash = urls
    lrc2 = lrcs
    name = titles + " - " +authors   
else:
    print("输入数字序号有误")
    print("程序已退出")
    exit()
url_data = requests.get(hash, headers=headers).content
with open(name + ".lrc", "w") as f:
    f.write(lrc2)
with open(name + ".mp3", "wb") as f:
    f.write(url_data)
    print("——————————")
    print("{} —— 已下载".format(name))

Qing 发表于 2021-1-26 19:39

谢谢@Thanks!

vapmire 发表于 2021-1-26 20:05


感谢分享,谢谢提供分享

spp123 发表于 2021-1-26 20:48

谢谢分享

qq745912121 发表于 2021-1-26 20:50

谢谢大佬

SCORPJON 发表于 2021-1-26 20:50

谢谢分享!

1557896756 发表于 2021-1-26 21:14

多谢分享多谢分享

Limber2333 发表于 2021-1-26 23:45

6666

peterzou2 发表于 2021-1-27 00:34

谢谢大牛

马了顶大 发表于 2021-1-27 08:36

谢谢分享
页: [1] 2
查看完整版本: Python抓取全网音乐