Python分析《孤注一擲》豆瓣評(píng)論數(shù)據(jù),看看它為什么值得看?

前言

嗨嘍~大家好呀,這里是魔王吶 ? ~!

環(huán)境使用

  • Python 3.8 解釋器

  • Pycharm 編輯器

所需模塊

  • import parsel >>> pip install parsel

  • import requests >>> pip install requests

  • import csv

代碼實(shí)現(xiàn)步驟:

基本四大步驟 --> 發(fā)送請(qǐng)求,獲取數(shù)據(jù),解析數(shù)據(jù),保存數(shù)據(jù)

  1. 發(fā)送請(qǐng)求, 模擬瀏覽器對(duì)于url地址發(fā)送請(qǐng)求

    https://movie.douban.com/subject/35267208/comments?limit=20&status=P&sort=new_score

  2. 獲取數(shù)據(jù), 獲取服務(wù)器返回響應(yīng)數(shù)據(jù)

    開(kāi)發(fā)者工具 --> response

  3. 解析數(shù)據(jù), 提取我們想要的數(shù)據(jù)內(nèi)容

    評(píng)論相關(guān)數(shù)據(jù)

  4. 保存數(shù)據(jù), 把數(shù)據(jù)內(nèi)容保存表格文件里面

評(píng)論數(shù)據(jù)獲取

發(fā)送請(qǐng)求, 模擬瀏覽器對(duì)于url地址發(fā)送請(qǐng)求

返回<Response [200]>表示請(qǐng)求成功

# 請(qǐng)求鏈接
url = f'https://movie.douban.com/subject/35267224/comments?start=20&limit=20&status=P&sort=new_score'
# 偽裝模擬
headers = {
    # User-Agent 用戶(hù)代理, 表示瀏覽器基本身份標(biāo)識(shí)
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
# 發(fā)送請(qǐng)求
response = requests.get(url=url, headers=headers)
print(response)

解析數(shù)據(jù)

解析方法:

  • 正則re --> 直接對(duì)于字符串?dāng)?shù)據(jù)進(jìn)行解析
  • css選擇器 --> 根據(jù)標(biāo)簽屬性提取數(shù)據(jù)
  • xpath節(jié)點(diǎn)提取 --> 根據(jù)標(biāo)簽節(jié)點(diǎn)提取數(shù)據(jù)

把獲取下來(lái)html字符串?dāng)?shù)據(jù) <response.text>, 轉(zhuǎn)成可解析對(duì)象

selector = parsel.Selector(response.text) 
# 第一次提取, 所有div標(biāo)簽
divs = selector.css('div.comment-item')
# for循環(huán)遍歷, 把列表里面元素一個(gè)一個(gè)提取出來(lái)
for div in divs:
    name = div.css('.comment-info a::text').get()  # 昵稱(chēng)
    rating = div.css('.rating::attr(title)').get()  # 推薦
    date = div.css('.comment-time::attr(title)').get()  # 時(shí)間
    area = div.css('.comment-location::text').get()  # 地區(qū)
    votes = div.css('.votes::text').get()  # 有用
    short = div.css('.short::text').get().replace('\n', '')  # 評(píng)論
    # 數(shù)據(jù)存字典里面
    dit = {
        '昵稱(chēng)': name,
        '推薦': rating,
        '時(shí)間': date,
        '地區(qū)': area,
        '有用': votes,
        '評(píng)論': short,
    }
    # 寫(xiě)入數(shù)據(jù)
    print(name, rating, date, area, votes, short)

保存數(shù)據(jù)

  • data.csv --> 文件名

  • mode=a --> 保存方式 追加保存

  • encoding=‘utf-8’ --> 編碼格式

  • newline --> 換行符

  • f --> 文件對(duì)象

f = open('data10.csv', mode='a', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
    '昵稱(chēng)',
    '推薦',
    '時(shí)間',
    '地區(qū)',
    '有用',
    '評(píng)論',
])
# 寫(xiě)入表頭
csv_writer.writeheader()

分析評(píng)論數(shù)據(jù)

導(dǎo)入模塊

import pandas as pd
import jieba
import wordcloud

讀取數(shù)據(jù)

df = pd.read_csv('data10.csv')
df.head()

推薦分布

import pyecharts.options as opts
from pyecharts.charts import Pie

data_pair = [list(z) for z in zip(evaluate_type, evaluate_num)]
完整源碼/教程/資料加V:qian97378免費(fèi)獲取
data_pair.sort(key=lambda x: x[1])

c = (
    Pie(init_opts=opts.InitOpts(bg_color="#2c343c"))
    .add(
        series_name="豆瓣影評(píng)",
        data_pair=data_pair,
        rosetype="radius",
        radius="55%",
        center=["50%", "50%"],
        label_opts=opts.LabelOpts(is_show=False, position="center"),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="推薦分布",
            pos_left="center",
            pos_top="20",
            title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
        ),
        legend_opts=opts.LegendOpts(is_show=False),
    )
    .set_series_opts(
        tooltip_opts=opts.TooltipOpts(
            trigger="item", formatter="{a} <br/>: {c} (u0z1t8os%)"
        ),
        label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"),
    )
)
c.render_notebook()

地區(qū)分布

import pyecharts.options as opts
from pyecharts.charts import Pie

data_pair = [list(z) for z in zip(area_type, area_num)]
data_pair.sort(key=lambda x: x[1])

d = (
    Pie(init_opts=opts.InitOpts(bg_color="#2c343c"))
    .add(
        series_name="豆瓣影評(píng)",
        data_pair=data_pair,
        rosetype="radius",
        radius="55%",
        center=["50%", "50%"],
        label_opts=opts.LabelOpts(is_show=False, position="center"),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="地區(qū)分布",
            pos_left="center",
            pos_top="20",
            title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
        ),
        legend_opts=opts.LegendOpts(is_show=False),
    )
    .set_series_opts(
        tooltip_opts=opts.TooltipOpts(
            trigger="item", formatter="{a} <br/>: {c} (u0z1t8os%)"
        ),
        label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"),
    )
)
d.render_notebook()

尾語(yǔ)

感謝你觀看我的文章吶~本次航班到這里就結(jié)束啦 ??

希望本篇文章有對(duì)你帶來(lái)幫助 ??,有學(xué)習(xí)到一點(diǎn)知識(shí)~

躲起來(lái)的星星??也在努力發(fā)光,你也要努力加油(讓我們一起努力叭)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容