import csv
import requests
import re
import json
data_list = []
url = "https://music.163.com/discover/toplist"
headers = {
'user-agent': '自己的UA'
}
response = requests.get(url, headers=headers)
html = response.content.decode("utf-8")
# print(html)
# result = re.findall('<a href="/song\?id=(.*?)">(.*?)</a>', html, re.S)
result = re.findall('<textarea id="song-list-pre-data" style="display:none;">(.*?)</textarea>', html, re.S)
# result=re.findall('"artists":\[\{"id":5773,"name":"谢霆锋","tns":\[],"alias":\[]}\],"alias":\[],"status":0,"ftype":0,"score":100.0,"copyrightId":2706568,"mvid":14490635,"transNames":null,"no":1,"publishTime":973008000000,"commentThreadId":"R_SO_4_168905","name":"活着Viva","id":168905,"type":0}',html,re.S)
res = json.loads(result[0])
# print(res[0]['artists'][0]['name']) #歌手
# print(res[0]['album']['name']) #歌名
# for i in result:
# print(i)
# print(res[10]['artists'][0]["name"]) #歌手
# a = ""
# item = res[10]['artists']
# print(res[10]['artists']) # 歌名
# for i in res[10]['artists']:
# a = a + i["name"] + "\\"
# print(a)
# print()
# print(res[1]['artists']) # 歌名
for data in res:
name = ""
num = 0
# print(data['artists'][0]['name'])
item = {}
for i in data['artists']:
name += i["name"] + "\\"
num += 1
if (num == 1):
name = data['artists'][0]['name']
item["personname"] = name
item["songname"] = data['name']
data_list.append(item)
print(data_list)
with open("wyy.csv", 'w', encoding='utf-8', newline='') as f:
wt = csv.DictWriter(f, fieldnames=['personname', "songname"])
wt.writeheader()
wt.writerows(data_list)