当前位置: 移动技术网 > IT编程>脚本编程>Python > Python如何爬取b站热门视频并导入Excel

Python如何爬取b站热门视频并导入Excel

2020年08月11日  | 移动技术网IT编程  | 我要评论
代码如下#encoding:utf-8import requestsfrom lxml import etreeimport xlwtimport os # 爬取b站热门视频信息def spider(

代码如下

#encoding:utf-8
import requests
from lxml import etree
import xlwt
import 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主','排名', '热度','播放量','评论数']
  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()

结果展示:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网