《演員請就位2》彈幕的情感傾向分析

最近小編的娛樂公衆號被《演員請就位2》刷屏了,這部綜藝的從開播開始導演的熱搜話題就一直不斷,咱們用 Python 分析一下這部綜藝的視頻彈幕看看你們都在吐糟些什麼。html

彈幕抓取

在騰訊視頻打開最新的第 8 期的上下兩期,在 Network 面板中搜索【danmu】,找到彈幕的連接 (https://mfm.video.qq.com/danmu?otype=json....)python

分析其中的請求參數能夠發現只有 timestamp 參數在以每次 30 的數字遞增,盲猜一波應該是視頻每 30 秒獲取一次彈幕包,其餘的請求參數能夠保持不變json

import csv
import requests
import json
import time
from pathlib import Path

def danmu():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.80 Safari/537.36'
    }
    # 彈幕連接,視頻長度(秒)
    urls = [['https://mfm.video.qq.com/danmu?otype=json&callback=&target_id=6208914107%26vid%3Do0035t7199o&session_key=63761%2C673%2C1606144955&timestamp={}&_=1606144949402', 7478],
            ['https://mfm.video.qq.com/danmu?otype=json&callback=&target_id=6208234802%26vid%3Da00352eyo25&session_key=111028%2C1191%2C1606200649&timestamp={}&_=1606200643186', 8610]]

    for url in urls:
        for page in range(15, url[1], 30):
            u = url[0].format(page)
            html = requests.get(u, headers=headers)
            result = json.loads(html.text, strict=False)
            time.sleep(1)
            danmu_list = []
            # 遍歷獲取目標字段
            for i in result['comments']:
                content = i['content']  # 彈幕內容
                danmu_list.append([content])
                print(content)
            csv_write(danmu_list)

def csv_write(tablelist):
    tableheader = ['彈幕內容']
    csv_file = Path('danmu.csv')
    not_file = not csv_file.is_file()
    with open('danmu.csv', 'a', newline='', errors='ignore') as f:
        writer = csv.writer(f)
        if not_file:
            writer.writerow(tableheader)
        for row in tablelist:
            writer.writerow(row)

抓到了 7W+ 的彈幕,文件爲 3M 大小api

情感分析

抓取到彈幕後,用騰訊雲的情感分析 API 分析彈幕的情感傾向是正面的仍是負面的亦或是中性情感安全

參考騰訊雲 https://cloud.tencent.com/document/sdk/Python 頁面獲取 SecretId 和 SecretKey 安全憑證,用 pip install tencentcloud-sdk-python 安裝騰訊雲的 SDK,遇到證書錯誤時用 sudo "/Applications/Python 3.6/Install Certificates.command" 命令安裝證書網絡

from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.nlp.v20190408 import nlp_client, models

import ssl
ssl._create_default_https_context=ssl._create_unverified_context

def nlp(text):
    try:
        cred = credential.Credential("xxx", "xxx")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "nlp.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = nlp_client.NlpClient(cred, "ap-guangzhou", clientProfile)

        req = models.SentimentAnalysisRequest()
        params = {
            "Text": text,
            "Mode": "3class"
        }
        req.from_json_string(json.dumps(params))

        resp = client.SentimentAnalysis(req)
        sentiment = {'positive': '正面', 'negative': '負面', 'neutral': '中性'}
        return sentiment[resp.Sentiment]
    except TencentCloudSDKException as err:
        print(err)

示例結果session

導演好感度

對於頻頻上熱搜的導演們觀衆對他們的感官是怎麼樣的,將情感分析結果轉換成你們對各個導演評價的百分比,並用 pyecharts 製做成圖表app

彈幕中對趙薇的負面評價達到 30%,爾冬升、趙薇、郭敬明的正面評價都差很少在 46% 左右,主持人大鵬的正面評價竟然是最高的,達到 59%,趙薇的彈幕量最多、陳凱歌彈幕數量是第二個,爾冬升的彈幕量不到 2000echarts

彈幕詞雲

將彈幕詞雲化,看看你們都在吐槽寫什麼編輯器

第一眼就看到了的秋褲兩個字

def ciyun():
    with open('danmu.csv') as f:
        with open('ciyun.txt', 'a') as ciyun_file:
            csv_reader = csv.reader(f)
            for row in csv_reader:
                ciyun_file.write(row[0])

    # 構建並配置詞雲對象w
    w = wordcloud.WordCloud(width=1000,
                            height=700,
                            background_color='white',
                            font_path="/System/Library/fonts/PingFang.ttc",
                            collocations=False,
                            stopwords={'的', '了','啊','我','很','是','好','這','都','不'})

    
    f = open('ciyun.txt', encoding='utf-8')
    txt = f.read()
    txtlist = jieba.lcut(txt)
    result = " ".join(txtlist)
    
    w.generate(result)

    w.to_file('演員請就位2.png')

總結

騰訊視頻彈幕的抓取比較簡單,每隔 30 秒發送一次請求獲取彈幕包。有興趣的朋友能夠嘗試其餘視頻網站的彈幕抓取,一塊兒努力進步每天向上。

 

注意:若是你是打算找python高薪工做的話。我建議你多寫點真實的企業項目積累經驗。否則工做都找不到,固然不少人沒進過企業,怎麼會存在項目經驗呢? 因此你得多找找企業項目實戰多練習下撒。若是你很懶不想找,也能夠進個人Python交流圈:1156465813。羣文件裏面有我以前在作開發寫過的一些真實企業項目案例。你能夠拿去學習,不懂均可以在裙裏找我,有空會耐心給你解答下。

 

如下內容無用,爲本篇博客被搜索引擎抓取使用
(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)
python 是幹什麼的 零基礎學 python 要多久 python 爲何叫爬蟲
python 爬蟲菜鳥教程 python 爬蟲萬能代碼 python 爬蟲怎麼掙錢
python 基礎教程 網絡爬蟲 python python 爬蟲經典例子
python 爬蟲
(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)
以上內容無用,爲本篇博客被搜索引擎抓取使用

相關文章
相關標籤/搜索