import requests
import csv
from bs4 import BeautifulSoup
data_list=[]
url="https://sc.chinaz.com/yinxiao/"
headers={'User-Agent':'自己的UA'}
response=requests.get(url,headers=headers)
html = response.content.decode('utf-8')
soup = BeautifulSoup(html,'lxml')
div_tag=soup.find_all("div",class_="container")[3]# print(div_tag)for div in div_tag.find_all("div",class_="right-head"):
div_list=div.find('a')
item={}
pre="https://sc.chinaz.com"
item["音乐名称"]=div_list.find('p').string.strip()
item["音乐链接"]=pre+div_list.get("href")# print(div_list.get("href"))# print(div_list.find('p').string.strip())# print("*"*50)
data_list.append(item)withopen("音乐.csv",'w', encoding='utf-8', newline='')as f:
wt = csv.DictWriter(f,["音乐名称","音乐链接"])
wt.writeheader()
wt.writerows(data_list)
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 +=1if(num ==1):
name = data['artists'][0]['name']
item["personname"]= name
item["songname"]= data['name']
data_list.append(item)print(data_list)withopen("wyy.csv",'w', encoding='utf-8', newline='')as f:
wt = csv.DictWriter(f, fieldnames=['personname',"songname"])
wt.writeheader()
wt.writerows(data_list)