Python爬虫

"Python高级玩法之爬虫"

Posted by QnGhonu on August 10, 2024

对于网易云的歌曲无法自由下载还要付费这件事,我很烦恼,刚好,我写了个Python脚本,可以自动下载音乐

免责声明:本程序仅供个人学习使用

废话不多说,直接上代码: (也可以点这里下载 -> py文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from lxml import etree
import requests
import json
from concurrent.futures import ThreadPoolExecutor
 
# 创建线程池
pool = ThreadPoolExecutor(max_workers=10)
# 请求头信息
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400"
}
def download(id, name):
    # 构造下载链接
    url = f'http://music.163.com/song/media/outer/url?id={id}'
    # 发送下载请求
    response = requests.get(url=url, headers=headers).content
    # 将响应内容写入文件
    with open(name+'.mp3', 'wb') as f:
        f.write(response)
    # 打印下载完成消息
    print(name, '下载完成')
def get_id(url):
    # 发送请求获取页面内容
    response = requests.get(url=url, headers=headers).text
    # 使用XPath解析页面
    page_html = etree.HTML(response)
    # 提取歌曲列表信息
    id_list = page_html.xpath('//textarea[@id="song-list-pre-data"]/text()')[0]
    # 解析歌曲列表信息,并逐个提交下载任务到线程池
    for i in json.loads(id_list):
        name = i['name']
        id = i['id']
        author = i['artists'][0]['name']
        pool.submit(download, id, name+'-'+author)
    # 关闭线程池
    pool.shutdown()
if __name__ == '__main__':
    # 用户输入歌曲关键词
    keyword = input("请输入歌曲名称:")
    # 构造搜索URL
    search_url = f'https://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s={keyword}&type=1&offset=0&total=true&limit=5'
    # 发送搜索请求并获取响应内容
    response = requests.get(url=search_url, headers=headers).json()
    # 提取歌曲列表
    song_list = response['result']['songs']
    # 遍历歌曲列表,逐个提交下载任务到线程池
    for song in song_list:
        name = song['name']
        id = song['id']
        author = song['artists'][0]['name']
        pool.submit(download, id, name+'-'+author)
    # 关闭线程池
    pool.shutdown()