1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import requests from lxml import etree import xlwt
res=requests.get("https://www.bilibili.com/ranking/all/0/0/30") html=etree.HTML(res.text) rank=html.xpath('//li[@class="rank-item"]/@data-rank') href=html.xpath('//li[@class="rank-item"]/div[2]/div[2]/a/@href') title=html.xpath('//li[@class="rank-item"]/div[2]/div[2]/a/text()') liulan=html.xpath('//li[@class="rank-item"]/div[2]/div[2]/div[1]/span[1]/text()') danmu=html.xpath('//li[@class="rank-item"]/div[2]/div[2]/div[1]/span[2]/text()') up=html.xpath('//li[@class="rank-item"]/div[2]/div[2]/div[1]/a/span/text()') score=html.xpath('//li[@class="rank-item"]/div[2]/div[2]/div[2]/div/text()')
list=list(zip(rank,href,title,liulan,danmu,up,score))
wbook=xlwt.Workbook() wsheet=wbook.add_sheet("sheet1") wsheet.write(0,0,label="排名") wsheet.write(0,1,label="链接") wsheet.write(0,2,label="标题") wsheet.write(0,3,label="播放量") wsheet.write(0,4,label="弹幕数") wsheet.write(0,5,label="up") wsheet.write(0,6,label="综合得分")
for i,j in enumerate(list): wsheet.write(i+1,0,j[0]) wsheet.write(i+1,1,j[1]) wsheet.write(i+1,2,j[2]) wsheet.write(i+1,3,j[3]) wsheet.write(i+1,4,j[4]) wsheet.write(i+1,5,j[5]) wsheet.write(i+1,6,j[6])
wbook.save("rank.xls")
|