import requests
from lxmlimport etree
import xlwt
#定义列表
all_info_list = []
def get_info(url):
res = requests.get(url)
html = etree.HTML(res.text)
infos=html.xpath('//div[@class="rightContentBox"]/ul/li')
for infoin infos:
pm = info.xpath('div[1]/p/text()')[0]
song = info.xpath('div[2]/a/text()')[0]
singer = info.xpath('div[3]/a/text()')[0]
info_list = [pm, song, singer]
all_info_list.append(info_list)
if __name__ =='__main__':
book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('Sheet1')
header = ['pm', 'song', 'singer']
for tin range(len(header)):
sheet.write(0, t, header[t])
url ='http://www.kuwo.cn/bang/index'
get_info(url)
#循环取列表值写入EXCEL
i =1
for listin all_info_list:
j =0
for datain list:
sheet.write(i, j, data)
j +=1
i +=1
book.save('C:/Users/madin/Desktop/kuwoTOP200.xls')