# -*- coding: UTF-8 -*-
import requests
import json
import re
import pymysql
import time
import redis
#写一个方法,输入一个歌手的singer_mid,将歌手的热歌前30名信息下载到数据库中
def DLsing(singer_mid):
#拼接歌手对应的html
singerhtml = r"https://c.y.qq.com/v8/fcg-bin/fcg_v8_singer_track_cp.fcg?g_tk=540729270&jsonpCallback=MusicJsonCallbacksinger_track&loginUin=953247216&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0&ct=24&singermid="+str(singer_mid)+r"&order=listen&begin=0&num=30&songstatus=1"
singerhtml = requests.get(singerhtml).text
c = singerhtml.count('musicData')
if c >= 30:
c = 30
for i in range(c):#一开始这里忽略了一个问题,有的歌手的歌曲数量不足,所以这里还不能写死,用singerhtml.count()查询出对应的数量,然后写进去即可,返回的最大值是30,为了预防万一加了个上限
html = re.compile('MusicJsonCallbacksinger_track\((.*)').findall(singerhtml)
html = json.loads(html[0])
html = html['data']['list'][i]['musicData']
albumid = html['albumid']
albummid = html['albummid']
albumname = html['albumname']
singerid = html['singer'][0]['id']
singermid = html['singer'][0]['mid']
singername = html['singer'][0]['name']
songmid = html['songmid']
songname = html['songname']
songid = html['songid']
sql_albumid = html['albumid']
sql_albummid = html['albummid'].strip()
sql_albumname = html['albumname'].strip()
sql_singerid = html['singer'][0]['id']
sql_singermid = html['singer'][0]['mid'].strip()
sql_singername = html['singer'][0]['name'].strip()
sql_songmid = html['songmid'].strip()
sql_songname = html['songname'].strip()
sql_songid = html['songid']
sqlselect = "select sing_mid from sing where sing_mid = '" + songmid + "'"
sqlinsert = "insert into qqmusic.sing (sing_name,sing_id,sing_mid,singer_id,singer_mid,singer_name,album_id,album_mid,album_name) value (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
#语句建议使用%s,否则可能会报错
# print("第" , i+1, "轮的插入语句为:", sqlinsert)
r = redis.Redis(host='localhost', port=6379, db=0)
a = r.lrange(sql_songmid, 1, 3)
if len(a)==0:
conn = pymysql.connect(host='127.0.0.1', port=13306, user='zh', passwd='123456', db='QQMusic')
cursor = conn.cursor()
effect_row = cursor.execute(sqlselect)
conn.commit()
if effect_row >= 1:
print("该歌曲在数据库内已经存在,歌曲名:", songname)
continue
else:
effect_row1 = cursor.execute(sqlinsert, (
str(sql_songname), str(sql_songid), str(sql_songmid), str(sql_singerid), str(sql_singermid),
str(sql_singername), str(sql_albumid), str(sql_albummid), str(sql_albumname)))
if effect_row1 == 1:
print("该歌曲不在数据库内,写入成功,歌曲名:", songname)
else:
print("写入数据库失败")
r.lpush(sql_songmid, sql_songname, sql_singername,sql_albumname)
print("歌曲信息已写入Redis")
i = i + 1
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
else:
a = r.lrange(sql_songmid, 0, 3)
print("歌曲名称为:",str(a[2], 'utf-8'))
print("歌手名称为:", str(a[1], 'utf-8'))
print("专辑名称为:", str(a[0], 'utf-8'))
print(singername,"的歌曲处理下载完毕")
# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=13306, user='zh', passwd='123456', db='QQMusic')
# 创建游标
cursor = conn.cursor()
selectsinger = "select id from singerlist order by id desc limit 1"
singlist_id = cursor.execute(selectsinger)
result = cursor.fetchall()
conn.commit()
sqlmax=result[0][0]
# print(sqlmax)
for i in range(sqlmax):
p=i+1
getsingersql = "select singer_mid from singerlist where id ="+str(p)
singlist_singer_mid = cursor.execute(getsingersql)
singlist_singer_mid = cursor.fetchall()
conn.commit()
# print(singlist_singer_mid)
postmid = singlist_singer_mid[0][0]
DLsing(postmid)
time.sleep(1)
print("已完成第",p,"轮")
# 关闭游标
cursor.close()
# 关闭连接
conn.close()