使用Python腳本分析瀏覽器的瀏覽記錄
夜深了,窗外的風帶著寒意,屋內(nèi)卻充滿了迷人的神秘氛圍。你知道的,他最近總是神神秘秘地盯著電腦屏幕,嘴角掛著不可言說的微笑。而當你走近,他就像觸電一樣迅速切換頁面,仿佛你剛才看到的內(nèi)容對他來說有著致命的吸引力。你隱隱感到,有什么不對勁——他到底在看什么?
你決定,不能再被蒙在鼓里了。你深吸一口氣,打開電腦,敲擊鍵盤,這一次,你要用Python腳本分析他的瀏覽記錄,看看他到底在偷偷瀏覽些什么不可告人的秘密。
第一章:破解瀏覽記錄的密碼
你首先開始了最基礎(chǔ)的工作——尋找谷歌瀏覽器的歷史記錄文件。通過搜索和查閱資料,你終于鎖定了谷歌瀏覽器的歷史記錄文件存儲路徑。你興奮地打開了路徑,發(fā)現(xiàn)原來他的所有瀏覽記錄都被悄無聲息地保存在那座名為 History 的數(shù)據(jù)庫里。
你用 sqlite3 庫,輕松地打開了這個數(shù)據(jù)庫文件,開始提取歷史記錄中的關(guān)鍵數(shù)據(jù)——URL、網(wǎng)頁標題、訪問次數(shù)等。你毫不費力地獲取了這些信息,就像打開了密碼鎖的一把鑰匙。你開始分析這些數(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()
# 查詢訪問歷史
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)了一個奇怪的現(xiàn)象——他的瀏覽歷史中充滿了關(guān)于某些特定網(wǎng)站的訪問記錄。你不禁開始懷疑,這些網(wǎng)頁背后究竟隱藏著什么秘密?你決定統(tǒng)計下他訪問最多的前10個網(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)計了每個網(wǎng)站的訪問次數(shù),結(jié)果讓你大吃一驚——他竟然頻繁訪問了這些網(wǎng)站,訪問次數(shù)最多的前10個網(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)異常情況,你決定進一步分析,看看他的興趣究竟在哪里。
第二章:情感分析——窺探他的心情
作為一名程序員,你知道,要揭開一個人的秘密,僅僅查看訪問記錄是不夠的。你需要更深入地了解每個網(wǎng)頁背后蘊藏的情感——也許,某個網(wǎng)頁的標題透露出了他那隱藏的情感傾向。
你拿起你的編程工具,調(diào)用 jieba 進行中文分詞,然后利用 SnowNLP 做情感分析。你開始解密每個網(wǎng)頁標題的情感波動,心情是激動還是消沉?結(jié)果,讓你大吃一驚——原來這些網(wǎng)頁帶有極強的情感傾向,或喜悅、或憤怒,甚至有些網(wǎng)頁標題背后隱藏著某種無法言喻的情感暗流。
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 進行情感分析
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()
# 計算平均情感得分
if sentiment_scores:
average_score = np.mean(sentiment_scores)
else:
average_score = 0
# 生成報告
print("情感分析報告:")
print(f"平均情感得分: {average_score}")
print(f"積極情感的標題數(shù)量: {positive_count}")
print(f"消極情感的標題數(shù)量: {negative_count}")
print(f"中性情感的標題數(shù)量: {neutral_count}")
if __name__ == "__main__":
analyze_sentiment()
也許是你多想了,通過計算你得知,平均情感得分是 0.65,這個得分表明整體上男朋友訪問頁面的標題所傳達出的情感傾向是較為積極的。
情感分析報告:
平均情感得分: 0.6503623448873965
積極情感的標題數(shù)量: 2256
消極情感的標題數(shù)量: 712
中性情感的標題數(shù)量: 1439
第三章:揭示真相——用詞云揭開秘密
情感分析的結(jié)果讓你更加強烈地感受到一種神秘的情緒波動。于是,你決定利用 jieba 分詞提取網(wǎng)頁標題中的關(guān)鍵詞,生成一個詞云圖。通過這些詞云圖,你希望找到訪問頻率最高的詞語,看看男朋友的心里到底有什么“秘密”在萌動。
你去掉了所有的無意義詞匯,比如“了”、“的”和“么”等,只保留了那些能夠反映興趣的關(guā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 = {"了", "的", "么", "是", "在", "有", "和", "就", "都", "而",
"及", "與", "也", "很", "并", "之", "其", "此", "但", "一個", "一些"}
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)注點。
你笑了,關(guān)上電腦,輕松地靠在椅背上,心里已經(jīng)沒有了疑慮。男朋友的秘密已經(jīng)在你掌控之中。
以上就是使用Python腳本分析瀏覽器的瀏覽記錄的詳細內(nèi)容,更多關(guān)于Python分析瀏覽記錄的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pytorch讀取圖像數(shù)據(jù)轉(zhuǎn)成opencv格式實例
這篇文章主要介紹了pytorch讀取圖像數(shù)據(jù)轉(zhuǎn)成opencv格式實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python中數(shù)據(jù)爬蟲requests庫使用方法詳解
本篇文章主要介紹了python中數(shù)據(jù)爬蟲requests庫使用方法詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Python使用scipy模塊實現(xiàn)一維卷積運算示例
這篇文章主要介紹了Python使用scipy模塊實現(xiàn)一維卷積運算,結(jié)合實例形式分析了scipy模塊的功能及使用scipy模塊進行一維卷積運算的相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
python中計算一個列表中連續(xù)相同的元素個數(shù)方法
今天小編就為大家分享一篇python中計算一個列表中連續(xù)相同的元素個數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

