url="https://www.bilibili.com/anime/index/#st=1&order=2&season_version=-1&area=-1&is_finish=-1©right=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&sort=0&page=1"
这里没找到,可以判定时异步加载
这里可以获取我们想要的数据
1 import xlwt 2 import requests 3 4 5 # 功能:实现B站番剧排名的爬取 6 7 8 def main(): 9 datalist = askUrl() 10 saveData(datalist) # 保存数据 11 12 13 def askUrl(): 14 url = "https://api.bilibili.com/pgc/season/index/result" 15 headers = { 16 "User - Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" 17 } 18 datalist = [] 19 for page in range(1,157): 20 params = { 21 'st': '1', 22 'order': '2', 23 'season_version': '-1', 24 'area': '-1', 25 'is_finish': '-1', 26 'copyright': '-1', 27 'season_status': '-1', 28 'season_month': '-1', 29 'year': '-1', 30 'style_id': '-1', 31 'sort': '0', 32 'page': str(page), 33 'season_type': '1', 34 'pagesize': '20', 35 'type': '1', 36 } 37 response = requests.get(url, params=params, headers=headers) 38 html = response.json() 39 data = html["data"]["list"] # 数据位置 40 for anime in data: 41 data = [] 42 title = anime["title"] # 番剧名字 43 data.append(title) 44 45 episode = anime["index_show"] # 番剧集数 46 data.append(episode) 47 48 link = anime["link"] # 番剧链接 49 data.append(link) 50 51 playVolume = anime["order"] # 番剧播放量 52 data.append(playVolume) 53 54 ifVip = anime["badge"] # 是否会员专享 55 if ifVip == "": 56 data.append("不是会员专享") 57 else: 58 data.append(ifVip) 59 60 datalist.append(data) # y一部番剧的所有信息 61 print("第%d页已爬取完毕!"%page) 62 return datalist 63 64 65 def saveData(datalist): 66 savepath = "B站番剧排名.xls" #表格路径 67 workbook = xlwt.Workbook(encoding="utf-8") # 创建workbook对象 68 # 创建一个worksheet对象,第二个参数是指单元格是否允许重设置,默认为False 69 worksheet = workbook.add_sheet(savepath,cell_overwrite_ok=True) # 创建工作表 70 col = ("番剧名字","番剧集数","番剧链接","番剧播放量","番剧是否会员专享") 71 for i in range(0,5): 72 # 设置列宽,计算列宽度:256是单元格基数*用数据长度*2最终的结果单元格的宽度和数据正好填满 73 # worksheet.col(i).width = 256 * len(col[i]) * 2 # 自适应列宽 74 worksheet.write(0,i,col[i]) # 第一行的数据(列名) 75 for i in range(0,3119): # 第几行 76 data = datalist[i] 77 for j in range(0,5): # 第几列 78 worksheet.write(i+1,j,data[j]) 79 workbook.save(savepath) 80 81 82 if __name__ == '__main__': 83 main()
2021-10-30