学了那么久python 还没爬过歌曲 九酷音乐不错
本帖最后由 lihu5841314 于 2021-6-24 21:32 编辑importrequests,re,os
import urllib.parse
import time
headers = {
'Referer': 'https://www.9ku.com/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
}
defstart_music():
while True:
name = input('请输入你要下载的歌曲名称:')
name1 =urllib.parse.quote(name)
url = f"https://baidu.9ku.com/song/?key={name1}"
try:
rep1 = requests.get(url=url,headers=headers).text
dowm_url ='https:'+ re.search(r'<a target="_blank" href="(?P<d_url>.*?)" class="down">下载</a>',rep1).group('d_url')
rep2 = requests.get(url=dowm_url,headers=headers).text
return rep2
except:
print('没有找到,请输入正确的歌曲名称')
continue
defmusic_down(resp):
tag_music = re.search(r'<a href="(?P<d2_url>.*?)" style="display:none">(?P<mus_name>.*?)Mp3下载</a>',resp)
music_url = tag_music.group('d2_url')
music_name = tag_music.group('mus_name')
print(music_url,music_name)
if not os.path.exists('music'):
os.mkdir('music')
path = 'music/'+ music_name + ".mp3"
resp = requests.get(url=music_url,headers=headers).content
with open(path,"wb")asf:
f.write(resp)
print(music_name,"下载完成")
defmain():
start = time.time()
resp = start_music()
music_down(resp)
print("一共耗时",time.time()-start)
if __name__ == '__main__':
main()importrequests,re,os
import urllib.parse
def main():
name = input('请输入你要下载的歌曲名称:')
name1 =urllib.parse.quote(name)
url = f"https://baidu.9ku.com/song/?key={name1}"
headers={
'Referer':'https://www.9ku.com/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
}
rep1 = requests.get(url=url,headers=headers).text
dowm_url ='https:'+ re.search(r'<a target="_blank" href="(?P<d_url>.*?)" class="down">下载</a>',rep1).group('d_url')
rep2 = requests.get(url=dowm_url,headers=headers).text
tag_music = re.search(r'<a href="(?P<d2_url>.*?)" style="display:none">(?P<mus_name>.*?)Mp3下载</a>',rep2)
music_url = tag_music.group('d2_url')
music_name = tag_music.group('mus_name')
print(music_url,music_name)
if not os.path.exists('music'):
os.mkdir('music')
path = 'music/'+ music_name + ".mp3"
resp = requests.get(url=music_url,headers=headers).content
with open(path,"wb")asf:
f.write(resp)
print(music_name,"下载完成")
if __name__ == '__main__':
main(){:301_1000:}
感谢大佬 这技术不错{:1_927:} 大佬有没有推荐的PYTHON课程啊 完全没有看懂 霍霍还有九酷这个东西 见识到了 谢谢分享,正在学习PY。 《python从入门到秃顶》玩的好啊。谢谢楼主的分享 judgecx 发表于 2021-6-25 07:58
霍霍还有九酷这个东西 见识到了
这里的歌不错 谢谢分享,看看能不能怕到喜欢的歌
页:
[1]
2