# -*- coding: utf-8 -*- import requests def get_html(): url='https://y.qq.com/n/yqq/toplist/4.html/'#下载这个 url='https://u.y.qq.com/cgi-bin/musics.fcg?-=getUCGI4291922759215392&g_tk=616243355&sign=zzafuse5325l3248cc01529e50f824d8db9aa53969e2dc&loginUin=999&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0&data=%7B%22detail%22%3A%7B%22module%22%3A%22musicToplist.ToplistInfoServer%22%2C%22method%22%3A%22GetDetail%22%2C%22param%22%3A%7B%22topId%22%3A4%2C%22offset%22%3A0%2C%22num%22%3A20%2C%22period%22%3A%222020-04-23%22%7D%7D%2C%22comm%22%3A%7B%22ct%22%3A24%2C%22cv%22%3A0%7D%7D' resp=requests.get(url) return resp.json()#类型是json不是text def parse_html(): result=[]#用于存数据库 resp_json=get_html() list_song=resp_json['detail']['data']['data']['song'] for item in list_song: result.append((item['rank'],item['title'],item['singerName']))#里面是无组 return result import mysql.connector#python插入多条数据,不用循环 def save_data(): mydb=mysql.connector.connect(host='localhost',user='root',passwd='root',database='py_test') #insert into database mycursor=mydb.cursor() sql='insert into song_table values (%s,%s,%s)' lists=parse_html() mycursor.exeutemany(sql,lists) mydb.commite() if __name__=='__main__': save_data()
python通过读json爬qq音乐并批量插入数据库
阅读:3356 输入:2020-04-23 17:05:11