使用Python腳本分析瀏覽器的瀏覽記錄
夜深了,窗外的風(fēng)帶著寒意,屋內(nèi)卻充滿了迷人的神秘氛圍。你知道的,他最近總是神神秘秘地盯著電腦屏幕,嘴角掛著不可言說的微笑。而當(dāng)你走近,他就像觸電一樣迅速切換頁面,仿佛你剛才看到的內(nèi)容對(duì)他來說有著致命的吸引力。你隱隱感到,有什么不對(duì)勁——他到底在看什么?
你決定,不能再被蒙在鼓里了。你深吸一口氣,打開電腦,敲擊鍵盤,這一次,你要用Python腳本分析他的瀏覽記錄,看看他到底在偷偷瀏覽些什么不可告人的秘密。
第一章:破解瀏覽記錄的密碼
你首先開始了最基礎(chǔ)的工作——尋找谷歌瀏覽器的歷史記錄文件。通過搜索和查閱資料,你終于鎖定了谷歌瀏覽器的歷史記錄文件存儲(chǔ)路徑。你興奮地打開了路徑,發(fā)現(xiàn)原來他的所有瀏覽記錄都被悄無聲息地保存在那座名為 History
的數(shù)據(jù)庫里。
你用 sqlite3
庫,輕松地打開了這個(gè)數(shù)據(jù)庫文件,開始提取歷史記錄中的關(guān)鍵數(shù)據(jù)——URL、網(wǎng)頁標(biāo)題、訪問次數(shù)等。你毫不費(fèi)力地獲取了這些信息,就像打開了密碼鎖的一把鑰匙。你開始分析這些數(shù)據(jù),心中升起了一絲不安:到底是哪些網(wǎng)站讓他如此神魂顛倒?
import sqlite3 # 獲取瀏覽器歷史記錄數(shù)據(jù)庫路徑 history_path = r"C:\Users\你的用戶名\AppData\Local\Google\Chrome\User Data\Default\History" # 連接到歷史記錄數(shù)據(jù)庫 conn = sqlite3.connect(history_path) cursor = conn.cursor() # 查詢?cè)L問歷史 cursor.execute("SELECT url, title, visit_count FROM urls") rows = cursor.fetchall() # 打印歷史記錄 for row in rows: print(row) # 關(guān)閉數(shù)據(jù)庫連接 conn.close()
在輸出的記錄中,你發(fā)現(xiàn)了一個(gè)奇怪的現(xiàn)象——他的瀏覽歷史中充滿了關(guān)于某些特定網(wǎng)站的訪問記錄。你不禁開始懷疑,這些網(wǎng)頁背后究竟隱藏著什么秘密?你決定統(tǒng)計(jì)下他訪問最多的前10個(gè)網(wǎng)站,看看這些網(wǎng)站背后究竟有怎樣的真相。
import sqlite3 import os from urllib.parse import urlparse from collections import Counter def get_top_10_domains(): # 獲取用戶目錄 user_profile = os.getenv('USERPROFILE') # 構(gòu)建歷史記錄數(shù)據(jù)庫的完整路徑 history_path = os.path.join(user_profile, r'AppData\Local\Google\Chrome\User Data\Default\History') domain_counts = Counter() try: # 連接歷史記錄數(shù)據(jù)庫 conn = sqlite3.connect(history_path) cursor = conn.cursor() # 執(zhí)行 SQL 查詢,從 urls 表中獲取 url 信息 cursor.execute("SELECT url FROM urls") results = cursor.fetchall() for result in results: url = result[0] # 解析 url 獲取域名 domain = urlparse(url).netloc if domain: domain_counts[domain] += 1 except sqlite3.Error as e: print(f"SQLite error occurred: {e}") finally: # 關(guān)閉數(shù)據(jù)庫連接 conn.close() # 獲取前 10 大訪問量最多的域名 top_10_domains = domain_counts.most_common(10) for i, (domain, count) in enumerate(top_10_domains): print(f"{i + 1}. {domain}, Count: {count}") if __name__ == "__main__": get_top_10_domains()
通過 Python 代碼,你快速地統(tǒng)計(jì)了每個(gè)網(wǎng)站的訪問次數(shù),結(jié)果讓你大吃一驚——他竟然頻繁訪問了這些網(wǎng)站,訪問次數(shù)最多的前10個(gè)網(wǎng)站如下:
1. mp.weixin.qq.com, Count: 453
2. image.baidu.com, Count: 436
3. www.baidu.com, Count: 323
4. www.bilibili.com, Count: 306
5. douyin.com, Count: 260
6. github.com, Count: 113
7. juejin.cn, Count: 94
8. jd.com, Count: 89
9. www.taobao.com, Count: 68
10. dev.epicgames.com, Count: 65
這都是些常見的網(wǎng)站,并沒有發(fā)現(xiàn)異常情況,你決定進(jìn)一步分析,看看他的興趣究竟在哪里。
第二章:情感分析——窺探他的心情
作為一名程序員,你知道,要揭開一個(gè)人的秘密,僅僅查看訪問記錄是不夠的。你需要更深入地了解每個(gè)網(wǎng)頁背后蘊(yùn)藏的情感——也許,某個(gè)網(wǎng)頁的標(biāo)題透露出了他那隱藏的情感傾向。
你拿起你的編程工具,調(diào)用 jieba
進(jìn)行中文分詞,然后利用 SnowNLP
做情感分析。你開始解密每個(gè)網(wǎng)頁標(biāo)題的情感波動(dòng),心情是激動(dòng)還是消沉?結(jié)果,讓你大吃一驚——原來這些網(wǎng)頁帶有極強(qiáng)的情感傾向,或喜悅、或憤怒,甚至有些網(wǎng)頁標(biāo)題背后隱藏著某種無法言喻的情感暗流。
import sqlite3 import os import jieba from snownlp import SnowNLP import numpy as np def analyze_sentiment(): # 獲取用戶目錄 user_profile = os.getenv('USERPROFILE') # 構(gòu)建歷史記錄數(shù)據(jù)庫的完整路徑 history_path = os.path.join(user_profile, r'AppData\Local\Google\Chrome\User Data\Default\History') positive_count = 0 negative_count = 0 neutral_count = 0 sentiment_scores = [] try: # 連接歷史記錄數(shù)據(jù)庫 conn = sqlite3.connect(history_path) cursor = conn.cursor() # 執(zhí)行 SQL 查詢,從 urls 表中獲取 title 信息 cursor.execute("SELECT title FROM urls") results = cursor.fetchall() for result in results: title = result[0] if title: # 使用 jieba 分詞 words = jieba.cut(title) title_text = " ".join(words) # 使用 SnowNLP 進(jìn)行情感分析 s = SnowNLP(title_text) sentiment_score = s.sentiments sentiment_scores.append(sentiment_score) if sentiment_score > 0.6: positive_count += 1 elif sentiment_score < 0.4: negative_count += 1 else: neutral_count += 1 except sqlite3.Error as e: print(f"SQLite error occurred: {e}") finally: # 關(guān)閉數(shù)據(jù)庫連接 conn.close() # 計(jì)算平均情感得分 if sentiment_scores: average_score = np.mean(sentiment_scores) else: average_score = 0 # 生成報(bào)告 print("情感分析報(bào)告:") print(f"平均情感得分: {average_score}") print(f"積極情感的標(biāo)題數(shù)量: {positive_count}") print(f"消極情感的標(biāo)題數(shù)量: {negative_count}") print(f"中性情感的標(biāo)題數(shù)量: {neutral_count}") if __name__ == "__main__": analyze_sentiment()
也許是你多想了,通過計(jì)算你得知,平均情感得分是 0.65,這個(gè)得分表明整體上男朋友訪問頁面的標(biāo)題所傳達(dá)出的情感傾向是較為積極的。
情感分析報(bào)告:
平均情感得分: 0.6503623448873965
積極情感的標(biāo)題數(shù)量: 2256
消極情感的標(biāo)題數(shù)量: 712
中性情感的標(biāo)題數(shù)量: 1439
第三章:揭示真相——用詞云揭開秘密
情感分析的結(jié)果讓你更加強(qiáng)烈地感受到一種神秘的情緒波動(dòng)。于是,你決定利用 jieba
分詞提取網(wǎng)頁標(biāo)題中的關(guān)鍵詞,生成一個(gè)詞云圖。通過這些詞云圖,你希望找到訪問頻率最高的詞語,看看男朋友的心里到底有什么“秘密”在萌動(dòng)。
你去掉了所有的無意義詞匯,比如“了”、“的”和“么”等,只保留了那些能夠反映興趣的關(guān)鍵詞。你開始生成詞云圖,期待著看到某些特別的詞語在圖中“躍然紙上”。這些詞,或許正是解開這個(gè)謎團(tuán)的關(guān)鍵。
import sqlite3 import os import jieba from wordcloud import WordCloud import matplotlib.pyplot as plt import numpy as np from PIL import Image def get_titles(): user_profile = os.getenv('USERPROFILE') history_path = os.path.join(user_profile, r'AppData\Local\Google\Chrome\User Data\Default\History') titles = [] try: conn = sqlite3.connect(history_path) cursor = conn.cursor() cursor.execute("SELECT title FROM urls") results = cursor.fetchall() for result in results: title = result[0] if title: titles.append(title) except sqlite3.Error as e: print(f"SQLite error occurred: {e}") finally: conn.close() return titles def load_stopwords(): stopwords = {"了", "的", "么", "是", "在", "有", "和", "就", "都", "而", "及", "與", "也", "很", "并", "之", "其", "此", "但", "一個(gè)", "一些"} return stopwords def process_titles(titles): stopwords = load_stopwords() all_words = [] for title in titles: words = jieba.cut(title) for word in words: if word not in stopwords: all_words.append(word) return all_words def generate_wordcloud(words): # 設(shè)置詞云的形狀為 16:9 的長方形 width = 1600 height = 900 mask = np.zeros((height, width), np.uint8) wordcloud = WordCloud(width=width, height=height, background_color='white', font_path="simhei.ttf", stopwords=load_stopwords(), mask=mask).generate(' '.join(words)) plt.figure(figsize=(16, 9)) plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show() if __name__ == "__main__": titles = get_titles() words = process_titles(titles) generate_wordcloud(words)
第四章:真相大白
通過這一步,你不僅僅揭開了男朋友瀏覽記錄中的關(guān)鍵詞,還通過詞云圖清晰地看到了他最感興趣的網(wǎng)站和內(nèi)容。這些詞云中,某些關(guān)鍵詞異常突出,仿佛在告訴你他潛在的興趣和關(guān)注點(diǎn)。
你笑了,關(guān)上電腦,輕松地靠在椅背上,心里已經(jīng)沒有了疑慮。男朋友的秘密已經(jīng)在你掌控之中。
以上就是使用Python腳本分析瀏覽器的瀏覽記錄的詳細(xì)內(nèi)容,更多關(guān)于Python分析瀏覽記錄的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python3使用迭代生成器實(shí)現(xiàn)減少內(nèi)存占用
這篇文章主要介紹了python3使用迭代生成器實(shí)現(xiàn)減少內(nèi)存占用的相關(guān)資料,需要的朋友可以參考下2021-05-05python 6.7 編寫printTable()函數(shù)表格打印(完整代碼)
這篇文章主要介紹了python 6.7 編寫一個(gè)名為printTable()的函數(shù) 表格打印,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Python實(shí)現(xiàn)命令行通訊錄實(shí)例教程
這篇文章主要介紹怎樣編寫了一段命令行通訊錄的小程序。下面是編寫的思路以及代碼,歡迎感興趣的同學(xué)交流探討。2016-08-08全面了解Python的getattr(),setattr(),delattr(),hasattr()
下面小編就為大家?guī)硪黄媪私釶ython的getattr(),setattr(),delattr(),hasattr()。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06Python實(shí)現(xiàn)SqlServer查詢結(jié)果并寫入多個(gè)Sheet頁的方法詳解
這篇文章主要為大家整理了兩個(gè)Python實(shí)現(xiàn)SqlServer查詢結(jié)果并寫入多個(gè)Sheet頁的方法,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-12-12