1 使用iTunes 備份iphone;
2在C:\Users\xxxx\AppData\Roaming\Apple Computer\MobileSync\Backup路徑中找到備份檔案夾
3打開備份檔案夾找到其中的Manifest.db檔案
這個檔案是SQLite資料檔案,用Navicat Premium建立一個SQLite,加載這個資料庫檔案就可以。
可以看到這個db檔案是一個資料庫,其中有2張表。
SELECT fileID FROM "Files" where relativePath like '%MM.sqlite';
使用這個語句獲得檔案ID,再通過這個id找到存儲微信聊天記錄的資料庫檔案。
有了這個資料庫檔案,我們可以直接通過python中的sqlite3子產品對它進行操作。
import sqlite3
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba
import os
import re
# 去掉停用詞
def remove_stop_words(f):
stop_words = ['你好', '已添加', '現在', '可以', '開始', '聊天', '目前', '群聊', '人數', '過多', '顯示', '群成員', '昵稱', '資訊頁', '關閉', '參與人', '還有', '嗯'
,'一條','消息','撤回','我們','一個','什麼','這個','答案','沒有','還是','就是','自己','知道','哈哈','哈哈哈','不是','你們','怎麼','但是','是以']
for stop_word in stop_words:
f = f.replace(stop_word, '')
return f
# 生成詞雲
def create_word_cloud(f):
# 設定本地的 simhei 字型檔案位置
FONT_PATH = r'C:\Windows\Fonts\simhei.ttf'
f = remove_stop_words(f)
cut_text = " ".join(jieba.cut(f,cut_all=False, HMM=True))
wc = WordCloud(
font_path=FONT_PATH,
max_words=100,
width=2000,
height=1200,
)
wordcloud = wc.generate(cut_text)
# 寫詞雲圖檔
wordcloud.to_file("wordcloud.jpg")
# 顯示詞雲檔案
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
def get_content_from_weixin():
# 建立資料庫連接配接
conn = sqlite3.connect("weixin.db")
# 擷取遊标
cur = conn.cursor()
# 建立資料表
# 查詢目前資料庫中的所有資料表
sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'Chat%' "
cur.execute(sql)
tables = cur.fetchall()
content = ''
for table in tables:
sql = "SELECT Message FROM " + table[0]
cur.execute(sql)
temp_result = cur.fetchall()
for temp in temp_result:
content = content + str(temp)
# 送出事務
conn.commit()
# 關閉遊标
cur.close()
# 關閉資料庫連接配接
conn.close()
return content
content = get_content_from_weixin()
# 去掉 HTML 标簽裡的内容
pattern = re.compile(r'<[^>]+?>',re.S)
content = pattern.sub('', content)
# 将聊天記錄生成詞雲
create_word_cloud(content)