Python爬虫,最新的B站弹幕和评论爬虫,你们要的冰冰来啦!

开发 前端
​最近想爬下B站的弹幕和评论,发现网上找到的教程基本都失效了,毕竟爬虫和反爬是属于魔高一尺、道高一丈的双方,程序员小哥哥们在网络的两端斗智斗勇,也是精彩纷呈。

最近想爬下B站的弹幕和评论,发现网上找到的教程基本都失效了,毕竟爬虫和反爬是属于魔高一尺、道高一丈的双方,程序员小哥哥们在网络的两端斗智斗勇,也是精彩纷呈。

当然了,对于爬虫这一方,爬取网站数据,一般目的都是比较明确的,比如我这里就是为了冰冰,废话不多说,开干!

获取弹幕数据

这里先声明一点,虽然网络上的整体教程都失效了,但是有一些步骤还是可以参考的,比如我们可以知道,对于弹幕数据,我们是可以通过如下的一个接口来获取的。

​https://comment.bilibili.com/xxxx.xml​

在浏览器打开可以看到如下:

图片

数据还是非常干净的,那么下一步就是看如何获取这个 xml 的 url 地址了,也就是如何获取 324768988 ID;

接下来我们搜索整个网页的源码,可以发现如下情况;

图片

也就是说,我们需要的 ID 是可以在 script 当中获取的,下面就来编写一个提取 script 内容的函数。

def getHTML_content(self):
        # 获取该视频网页的内容
        response = requests.get(self.BVurl, headers = self.headers)
        html_str = response.content.decode()
        html=etree.HTML(html_str)
        result=etree.tostring(html)
        return result

def get_script_list(self,str):
    html = etree.HTML(str)
    script_list = html.xpath("//script/text()")
    return script_list
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

拿到所有的 script 内容之后,我们再来解析我们需要的数据。

script_list = self.get_script_list(html_content)
# 解析script数据,获取cid信息
for script in script_list:
        if '[{"cid":' in script:
            find_script_text = script
final_text = find_script_text.split('[{"cid":')[1].split(',"page":')[0]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

最后,我们再把整体代码封装成一个类,就完成了弹幕抓取的数据收集工作了。

spider = BiliSpider("BV16p4y187hc")
spider.run()
  • 1.
  • 2.

结果如下:

图片

获取评论数据

对于评论数据,可能要复杂一些,需要分为主(main)评论和回复主评论的 reply 评论。

我们通过浏览器工具抓取网页上的所有请求,然后搜索 reply,可以得到如下结果:

图片

我们先来看看 main 请求,整理后通过浏览器访问如下:

图片

也可以直接通过 requests 请求;

图片

通过观察可以得知,响应消息里的 replies 就是主评论内容,同时我们还可以改变 url 当中的 next 参数来翻页,进而请求不同的数据。

这里我们再关注下 rpid 参数,这个会用于 reply 评论中。

再来看看 reply 评论,同样可以使用 requests 直接访问,同时 url 当中的 root 参数就是我们上面提到的 rpid 参数。

图片

我们厘清了上面的关系之后,我们就可以编写代码了;

def get_data(data):
    data_list = []
    comment_data_list = data["data"]["replies"]
    for i in comment_data_list:
        data_list.append([i['rpid'], i['like'], i['member']['uname'], i['member']['level_info']['current_level'], i['content']['message']])
    return data_list


def save_data(data_type, data):
    if not os.path.exists(data_type + r'_data.csv'):
        with open(data_type + r"_data.csv", "a+", encoding='utf-8') as f:
            f.write("rpid,点赞数量,用户,等级,评论内容\n")
            for i in data:
                rpid = i[0]
                like_count = i[1]
                user = i[2].replace(',', ',')
                level = i[3]
                content = i[4].replace(',', ',')
                row = '{},{},{},{},{}'.format(rpid,like_count,user,level,content)
                f.write(row)
                f.write('\n')
    else:
        with open(data_type + r"_data.csv", "a+", encoding='utf-8') as f:
            for i in data:
                rpid = i[0]
                like_count = i[1]
                user = i[2].replace(',', ',')
                level = i[3]
                content = i[4].replace(',', ',')
                row = '{},{},{},{},{}'.format(rpid,like_count,user,level,content)
                f.write(row)
                f.write('\n')


for i in range(1000):
    url = "https://api.bilibili.com/x/v2/reply/main?jsnotallow=jsonp&next={}&type=1&oid=972516426&mode=3&plat=1&_=1632192192097".format(str(i))
    print(url)
    d = requests.get(url)
    data = d.json()
    if not data['data']['replies']:
        break
    m_data = get_data(data)
    save_data("main", m_data)
    for j in m_data:
        reply_url = "https://api.bilibili.com/x/v2/reply/reply?jsnotallow=jsonp&pn=1&type=1&oid=972516426&ps=10&root={}&_=1632192668665".format(str(j[0]))
        print(reply_url)
        r = requests.get(reply_url)
        r_data = r.json()
        if not r_data['data']['replies']:
            break
        reply_data = get_data(r_data)
        save_data("reply", reply_data)
        time.sleep(5)
    time.sleep(5)
  • 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.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.

爬取过程中:

图片

这样,针对一个冰冰视频,我们就完成了上千评论的抓取;

可视化

下面我们简单做一些可视化动作;

先来看下我们爬取的数据整体的样子:

图片

因为数据中有一些空值,我们来处理下:

df_new = df.dropna(axis=0,subset = ["用户"])
  • 1.

下面就可以作图了,GO!

使用 pyecharts 还是我们的首选,毕竟编写容易

评论热度

df1 = df.sort_values(by="点赞数量",ascending=False).head(20)

c1 = (
    Bar()
    .add_xaxis(df1["评论内容"].to_list())
    .add_yaxis("点赞数量", df1["点赞数量"].to_list(), color=Faker.rand_color())
    .set_global_opts(
        title_opts=opts.TitleOpts(title="评论热度Top20"),
        datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")],
    )
    .render_notebook()
)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

图片

等级分布

pie_data = df_new.等级.value_counts().sort_index(ascending=False)
pie_data.tolist()
c2 = (
    Pie()
    .add(
        "",
        [list(z) for z in zip([str(i) for i in range(6, 1, -1)], pie_data.tolist())],
        radius=["40%", "75%"],
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="等级分布"),
        legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .render_notebook()
)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

图片

评论词云

def wordcloud(data, name, pic=None):
    comment = jieba.cut(str(data), cut_all=False)
    words = ' '.join(comment)
    img = Image.open(pic)
    img_array = np.array(img)
    wc = WordCloud(width=2000, height=1800, background_color='white', font_path=font, mask=img_array,
                   stopwords=STOPWORDS, contour_width=3, contour_color='steelblue')
    wc.generate(words)
    wc.to_file(name + '.png')

wordcloud(df_new["评论内容"], "冰冰", '1.PNG')
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

图片

好了,今天的分享就到这里,喜欢冰冰就点个在看吧!

责任编辑:武晓燕 来源: 萝卜大杂烩
相关推荐

2018-01-04 09:20:55

python爬虫视频弹幕

2017-08-09 15:27:33

python爬虫开发工具

2021-11-09 09:46:09

ScrapyPython爬虫

2021-11-08 14:38:50

框架Scrapy 爬虫

2018-01-30 18:15:12

Python网络爬虫gevent

2018-01-09 18:06:41

Python爬虫技巧

2020-10-19 19:25:32

Python爬虫代码

2018-06-05 09:13:47

2024-06-07 08:56:43

HTTPPythonSelenium

2020-11-05 09:26:55

Cookie和Sess

2016-10-21 14:35:52

Pythonwebget方法

2016-10-13 15:51:50

2016-10-20 20:21:09

Python爬虫技巧

2019-06-18 10:49:41

Python技术web

2018-01-29 09:28:44

2021-08-10 09:01:48

Python网络爬虫自动化

2019-03-22 08:25:20

x86PythonARM

2024-03-08 12:17:39

网络爬虫Python开发

2017-08-22 17:30:14

Python爬虫
点赞
收藏

51CTO技术栈公众号