python爬虫批量下载qq音乐
时间: 2023-12-20 08:05:21 浏览: 209
ppt模板批量下载脚本.7z
以下是使用Python爬虫批量下载QQ音乐的示例代码:
```python
import requests
import os
def download_song(url, save_path):
response = requests.get(url)
with open(save_path, 'wb') as file:
file.write(response.content)
def batch_download_songs(song_list, save_dir):
if not os.path.exists(save_dir):
os.makedirs(save_dir)
for song in song_list:
song_name = song['name']
song_url = song['url']
save_path = os.path.join(save_dir, f'{song_name}.mp3')
download_song(song_url, save_path)
print(f'Downloaded: {song_name}')
# 示例使用的歌曲列表
song_list = [
{'name': '夏天的风', 'url': 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=55043609442258168&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=10&w=夏天的风&g_tk_new_20200303=5381&g_tk=5381&loginUin=229554158&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0'},
{'name': '夜曲', 'url': 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=55043609442258168&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=10&w=夜曲&g_tk_new_20200303=5381&g_tk=5381&loginUin=229554158&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0'}
]
# 设置保存目录
save_directory = 'songs'
# 批量下载歌曲
batch_download_songs(song_list, save_directory)
```
请注意,示例中的歌曲列表和保存目录需要根据实际情况进行修改。此外,为了避免侵权问题,请确保你有合法的下载权限。
阅读全文