国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python如何爬取b站熱門視頻并導入Excel

瀏覽:2日期:2022-07-14 14:38:37

代碼如下

#encoding:utf-8import requestsfrom lxml import etreeimport xlwtimport os # 爬取b站熱門視頻信息def spider(): video_list = [] url = 'https://www.bilibili.com/ranking?spm_id_from=333.851.b_7072696d61727950616765546162.3' html = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}).text html = etree.HTML(html) infolist = html.xpath('//li[@class=’rank-item’]') for item in infolist: rank = ''.join(item.xpath('./div[@class=’num’]/text()')) video_link = ''.join(item.xpath('.//div[@class=’info’]/a/@href')) title = ''.join(item.xpath('.//div[@class=’info’]/a/text()')) payinfo = ''.join(item.xpath('.//div[@class=’detail’]/span/text()')).split('萬') play = payinfo[0] + '萬' comment = payinfo[1] if comment.isdigit() == False: comment += '萬' upname = ''.join(item.xpath('.//div[@class=’detail’]/a/span/text()')) uplink = 'http://' + ''.join(item.xpath('.//div[@class=’detail’]/a/@href')) hot = ''.join(item.xpath('.//div[@class=’pts’]/div/text()')) video_list.append({ ’rank’: rank, ’videolink’: video_link, ’title’: title, ’play’: play, ’comment’: comment, ’upname’: upname, ’uplink’: uplink, ’hot’: hot }) return video_list def write_Excel(): # 將爬取的信息添加到Excel video_list = spider() workbook = xlwt.Workbook() # 定義表格 sheet = workbook.add_sheet('b站熱門視頻') # 添加sheet的name xstyle = xlwt.XFStyle() # 實例化表格樣式對象 xstyle.alignment.horz = 0x02 # 字體居中 xstyle.alignment.vert = 0x01 head = [’視頻名’, ’up主’,’排名’, ’熱度’,’播放量’,’評論數(shù)’] for h in range(len(head)): sheet.write(0, h, head[h], xstyle) i = 1 for item in video_list: # 向單元格(視頻名)添加該視頻的超鏈接 if ’'’ in item['title']: item['title'] = item['title'].split(’'’)[1] title_data = ’HYPERLINK('’+item['videolink']+’';'’+item['title']+’')’ # 設置超鏈接 sheet.col(0).width = int(256 * len(title_data) * 3/5) # 設置列寬 sheet.write(i, 0, xlwt.Formula(title_data), xstyle) name_data = ’HYPERLINK('’+item['uplink']+’';'’+item['upname']+’')’ sheet.col(1).width = int(256 * len(name_data) * 3/5) sheet.write(i, 1, xlwt.Formula(name_data), xstyle) sheet.write(i, 2, item['rank'], xstyle) sheet.write(i, 3, item['hot'], xstyle) sheet.write(i, 4, item['play'], xstyle) sheet.write(i, 5, item['comment'], xstyle) i += 1 # 如果文件存在,則將其刪除 file = 'b站熱門視頻信息.xls' if os.path.exists(file): os.remove(file) workbook.save(file) if __name__ == ’__main__’: write_Excel()

結果展示:

Python如何爬取b站熱門視頻并導入Excel

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

相關文章:
主站蜘蛛池模板: 通州区| 建昌县| 寿光市| 久治县| 游戏| 斗六市| 琼中| 新乡县| 东乡族自治县| 穆棱市| 勃利县| 邳州市| 大余县| 胶南市| 广安市| 壤塘县| 揭西县| 阿坝县| 浮梁县| 龙南县| 玛沁县| 炎陵县| 洛阳市| 安化县| 甘德县| 石台县| 云林县| 鹤山市| 张家口市| 微博| 比如县| 松原市| 巩义市| 加查县| 东乡族自治县| 东源县| 安宁市| 昭觉县| 枣阳市| 蒙自县| 布尔津县|