[Python] 纯文本查看 复制代码 # coding: utf-8
import requests
import csv
import re
with open("网易云音乐排行榜.csv",mode="w",encoding="utf-8") as f:
csv_writer = csv.writer(f)
csv_writer.writerow(['音乐名','网址'])
# 注意headers里面的大小写
headers = {'referer': 'https://music.163.com/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}
url = 'https://music.163.com/discover/toplist'
response = requests.get(url=url, headers=headers)
print(response.text)
title_herf = re.findall('<li><a href="/(.*?)">(.*?)</a></li>', response.text)
for i in title_herf:
title = i[1]
herf ="https://music.163.com/#/"+ i[0]
print(title, herf)
with open("网易云音乐排行榜.csv", mode="a", encoding="utf-8") as f:
csv_writer = csv.writer(f)
csv_writer.writerow([title, herf])
'''
https://music.163.com/#/song?id=1981889746
'''
|