データベースの歌時計の中のすべての歌手の上位30位の熱い歌を取得して、データベースとRedisの中で保存します
4705 ワード
# -*- 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()