#爬取豆瓣音乐专辑排行榜中专辑名称和所属类别
import requests
from lxmlimport etree
import xlwt
all_info_list = []
url ='https://music.douban.com/'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36'
}
res = requests.get(url)
html = etree.HTML(res.text)
infos = html.xpath('//div[@class="artist-item"]')
for infoin infos:
special = info.xpath('a/text()')
kind = info.xpath('p/text()')
info_list = [special,kind]
all_info_list.append(info_list)
if __name__ =='__main__':
book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('Sheet1')
header = ['special', 'kind']
for tin range(len(header)):
sheet.write(0, t, header[t])
i =1
for list in all_info_list:
j =0
for data in list:
sheet.write(i, j, data)
j +=1
i +=1
book.save('C:/Users/madin/Desktop/test.xls')