Python爬取新型冠狀病毒“謠言”新聞進(jìn)行數(shù)據(jù)分析
一、爬取數(shù)據(jù)
話不多說了,直接上代碼( copy即可用 )
import requests
import pandas as pd
class SpiderRumor(object):
def __init__(self):
self.url = "https://vp.fact.qq.com/loadmore?artnum=0&page=%s"
self.header = {
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
}
def spider_run(self):
df_all = list()
for url in [self.url % i for i in range(30)]:
data_list = requests.get(url, headers=self.header).json()["content"]
temp_data = [[df["title"], df["date"], df["result"], df["explain"], df["tag"]] for df in data_list]
df_all.extend(temp_data)
print(temp_data[0])
pd.DataFrame(df_all, columns=["title", "date", "result", "explain", "tag"]).to_csv("冠狀病毒謠言數(shù)據(jù).csv", encoding="utf_8_sig")
if __name__ == '__main__':
spider = SpiderRumor()
spider.spider_run()
爬蟲過程

二、數(shù)據(jù)分析
數(shù)據(jù)展示

每日謠言數(shù)量

由圖可得:1月24日和1月25日是謠言的高峰期,讓我們來看看這兩天的數(shù)據(jù):


由上圖得知 一月二十四號和二十號傳播的 29 條謠言中 96.55% 都是假的
謠言是否屬實(shí)占比

從1月18日到今日截止2月14日共發(fā)現(xiàn)了300條謠言,右上圖可得:76.33% 都是假的,只要 7.00% 是屬實(shí)的,其中 14.33% 的謠言屬于 偽科學(xué) 而且 還有 8.00% 屬于尚無定論憑空捏造出的,需要多注意⚠️
謠言的關(guān)鍵字展示

下面介紹 matplotlib 繪制餅圖的代碼
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Windows系統(tǒng)設(shè)置中文字體
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_csv("/冠狀病毒謠言數(shù)據(jù).csv"")
labels = data["explain"].value_counts().index.tolist()
sizes = data["explain"].value_counts().values.tolist()
colors = ['lightgreen', 'gold', 'lightskyblue', 'lightcoral']
plt.figure(figsize=(15,8))
plt.pie(sizes, labels=labels,
colors=colors, autopct='%1.1f%%', shadow=True, startangle=50) # shadow=True 表示陰影
plt.axis('equal') # 使圖居中
plt.show()
繪制謠言關(guān)鍵字分布圖(觀察 tag 這個字段)

由于 tag 這個字段內(nèi)容是列表,我們?nèi)〕鰜砗笫橇斜砬短琢斜恚篬[a, b], [b, c], [c, d]] 我們要使用一行列表生成式快速的將所以的關(guān)鍵字取出來 [j for i in a for j in i]
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Windows系統(tǒng)設(shè)置中文字體
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_csv("/冠狀病毒謠言數(shù)據(jù).csv"")
df = pd.Series([j for i in [eval(i) for i in data["tag"].tolist()] for j in i]).value_counts()[:20]
X = df.index.tolist()
y = df.values.tolist()
plt.figure(figsize=(15, 8)) # 設(shè)置畫布
plt.bar(X, y, color="orange")
plt.tight_layout()
# plt.grid(axis="y")
plt.grid(ls='-.')
plt.show()
總結(jié)
以上所述是小編給大家介紹的Python爬取新型冠狀病毒“謠言”新聞進(jìn)行數(shù)據(jù)分析,希望對大家有所幫助!
- Python 數(shù)據(jù)分析之逐塊讀取文本的實(shí)現(xiàn)
- Python數(shù)據(jù)分析庫pandas高級接口dt的使用詳解
- 詳解python爬取彈幕與數(shù)據(jù)分析
- Python Pandas數(shù)據(jù)分析工具用法實(shí)例
- 大數(shù)據(jù)分析用java還是Python
- python 數(shù)據(jù)分析實(shí)現(xiàn)長寬格式的轉(zhuǎn)換
- python數(shù)據(jù)分析工具之 matplotlib詳解
- Python實(shí)現(xiàn)的北京積分落戶數(shù)據(jù)分析示例
- 基于Python數(shù)據(jù)分析之pandas統(tǒng)計分析
- python數(shù)據(jù)分析:關(guān)鍵字提取方式
- Python數(shù)據(jù)分析pandas模塊用法實(shí)例詳解
- python 繪制斜率圖進(jìn)行對比分析
相關(guān)文章
python實(shí)現(xiàn)人人網(wǎng)登錄示例分享
這篇文章主要介紹了python實(shí)現(xiàn)登錄人人網(wǎng)示例,大家參考使用吧2014-01-01
Python實(shí)現(xiàn)監(jiān)控程序執(zhí)行時間并將其寫入日志的方法
這篇文章主要介紹了Python實(shí)現(xiàn)監(jiān)控程序執(zhí)行時間并將其寫入日志的方法,實(shí)例分析了Python日志操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Django上使用數(shù)據(jù)可視化利器Bokeh解析
這篇文章主要介紹了Django上使用數(shù)據(jù)可視化利器Bokeh解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
CentOS7上使用pyenv搭建Django環(huán)境
本文主要介紹了CentOS7上使用pyenv搭建Django環(huán)境,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Python 實(shí)時獲取任務(wù)請求對應(yīng)的Nginx日志的方法
本文給大家分享Python 實(shí)時獲取任務(wù)請求對應(yīng)的Nginx日志的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-07-07
Window 64位下python3.6.2環(huán)境搭建圖文教程
這篇文章主要為大家詳細(xì)介紹了Window 64位下python3.6.2環(huán)境搭建圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09
python實(shí)現(xiàn)快遞價格查詢系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)快遞價格查詢系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
Python制作數(shù)據(jù)預(yù)測集成工具(值得收藏)
這篇文章主要介紹了Python如何制作數(shù)據(jù)預(yù)測集成工具,幫助大家進(jìn)行大數(shù)據(jù)預(yù)測,感興趣的朋友可以了解下2020-08-08

