python获取酷狗TOP500音乐排行榜(新手向)

import requests
from bs4 import BeautifulSoup
import xlwt

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'}

url = "https://www.kugou.com/yy/rank/home/1-8888.html?from=rank"

html_code = requests.get(url,headers=headers)

html_code.encoding = "utf-8"

# print(html_code.text)

soup = BeautifulSoup(html_code.text,"html.parser")

li_all = soup.find(class_="pc_temp_songlist").find_all("li")

song_name = []
song_duration = []

for li_s in li_all:
    song_name.append(li_s.get("title"))

for s_d in soup.find_all("span",attrs={"class":"pc_temp_time"}):
    song_duration.append(s_d.get_text().replace("\n","").replace("\t",""))

print(song_name,song_duration)

print(len(song_name),len(song_duration))

head = ["歌曲名称","歌曲时长"]

workbook = xlwt.Workbook()

sheet = workbook.add_sheet("sheet1")

sheet.col(0).width = 8000

for i in range(len(head)):
    sheet.write(0,i,head[i])

for i in range(len(song_name)):
    sheet.write(i+1, 0, song_name[i])

for i in range(len(song_duration)):
    sheet.write(i+1, 1, song_duration[i])

workbook.save("test.xlsx")

写出excel截图:

python获取酷狗TOP500音乐排行榜(新手向)_第1张图片

发现这篇文章的代码挺好的:

https://blog.csdn.net/springhammer/article/details/88650068 

 

你可能感兴趣的:(excel,python,爬虫)