$python spiderKugou.py #默认显示结果20条
$python spiderKugou.py 30 #设置显示结果为30条
然后输入歌手名或歌曲名得到结果
最后再输入歌曲序号即可下载
import json
from urllib.parse import urlencode
#import pymongo
import requests
from requests.exceptions import ConnectionError
import re
import sys
def get_page_index(keyword,pagesize):
data = {
'callback':'jQuery112408098406646795082_1546606289402',
'keyword':keyword,
'page':1,
'pagesize':pagesize,
'userid':-1,
'clientver':'',
'platform':'WebFilter',
'tag':'em',
'filter':2,
'iscorrection':1,
'privilege_filter':0
}
url="https://songsearch.kugou.com/song_search_v2?" + urlencode(data)
#print(url)
try:
response = requests.get(url)
if response.status_code == 200:
#print(response.text)
return response.text
return None
except ConnectionError:
print('Error occurred')
return None
def parse_page_index(html,pagesize):
temp = re.match('^jQuery.*?\((.*?)\)$', html)
data = json.loads(temp.group(1))
#print(data)
lists=data.get('data').get('lists')
if len(lists) < pagesize :
pagesize = len(lists)
for i in range(pagesize):
print(str(i+1)+". "+str(lists[i]['FileName']).replace('<em>','').replace('</em>',''))
print('\n[如果需要下载多首,请用空格隔开] (输入0退出)')
numbers = list(map(int, input("请输入需要下载的歌曲序号:").split(' ')))
if numbers[0]==0:
return None
for number in numbers:
name = str(lists[number-1]['FileName']).replace('<em>','').replace('</em>','').replace(' ','')
print(name)
hash_url = 'http://www.kugou.com/yy/index.php?r=play/getdata&hash='+lists[number-1].get('FileHash')
hash_content = requests.get(hash_url)
play_url = ''.join(re.findall('"play_url":"(.*?)"', hash_content.text))
real_download_url = play_url.replace("\\","")
print('正在下载...')
with open("./music/"+name+".mp3","wb") as fp:
fp.write(requests.get(real_download_url).content)
print("Download successfully!\n")
def main():
keyword = input("请输入歌手名或歌曲名:")
pagesize = 20 #指定搜索结果显示歌曲的数量
if len(sys.argv) > 1:
pagesize = int(sys.argv[1])
html = get_page_index(keyword,pagesize)
parse_page_index(html,pagesize)
if __name__ == '__main__':
main()