用 Python 幫小伙伴找到頭上一片綠的證據(jù)!
本文轉(zhuǎn)載自微信公眾號(hào)「Python技術(shù)」,作者派森醬 。轉(zhuǎn)載本文請(qǐng)聯(lián)系Python技術(shù)公眾號(hào)。
這周末有個(gè)小伙伴找到派森醬,說(shuō)他女票這幾天整天都在上網(wǎng),也不知道瀏覽什么內(nèi)容,只要這個(gè)小伙伴湊上去瞧瞧就只看見(jiàn)了桌面,查看瀏覽器歷史記錄也被刪除的一干二凈。小伙伴有時(shí)候覺(jué)得自己頭上已經(jīng)是一片綠,想讓派森醬弄個(gè) python 程序找點(diǎn)實(shí)錘線(xiàn)索。
小編就花了一點(diǎn)時(shí)間寫(xiě)了一個(gè)讀取瀏覽器歷史的小腳本,并教他如何隱藏小腳本并且使用 windows 自帶的任務(wù)調(diào)度 3 分鐘一次自動(dòng)運(yùn)行。
browserhistory
browserhistory 是一個(gè)可以很方便的獲取瀏覽器歷史記錄的第三方模塊,支持 safari、chrome、firefox 瀏覽器。
- pip install browserhistory
使用
先來(lái)看看 Chrome 瀏覽器的歷史記錄存放在磁盤(pán)的哪個(gè)地方, 在瀏覽器地址欄輸入 chrome://version,如下圖可以找到 Chrome 將個(gè)人資料存放的地址。
其中 history 文件就是歷史記錄,它是一個(gè) sqlite 數(shù)據(jù)庫(kù)文件,可以使用 DB Browser for SQLite (https://sqlitebrowser.org/dl/) 工具打開(kāi)并查詢(xún)數(shù)據(jù)。
下面三行代碼調(diào)用 browserhistory 模塊獲取歷史,并保存在了 CSV 文件中。
- import browserhistory as bh
- dict_obj = bh.get_browserhistory()
- bh.write_browserhistory_csv()
統(tǒng)計(jì)
用 Excel 看瀏覽的網(wǎng)站數(shù)據(jù)并不是很直觀(guān),可以使用 pycharts 模塊生成餅圖查看點(diǎn)擊次數(shù)最高的前十次網(wǎng)站。
- import csv
- from urllib import parse
- from pyecharts import options as opts
- from pyecharts.charts import Pie
- hostname_dic = {}
- with open("chrome_history.csv", encoding="utf-8") as csvfile:
- csv_reader = csv.reader(csvfile)
- birth_header = next(csv_reader)
- for row in csv_reader:
- hostname = parse.urlparse(row[0]).hostname
- hostname_dic[hostname] = hostname_dic.get(hostname, 0) + 1
- sorted(hostname_dic.items(),key = lambda x:x[1],reverse = True)
- c = (
- Pie()
- .add(
- "",
- [
- list(z)
- for z in zip(
- list(hostname_dic)[0:10],
- list(hostname_dic.values())[0:10],
- )
- ],
- center=["40%", "50%"],
- )
- .set_global_opts(
- title_opts=opts.TitleOpts(title="歷史記錄"),
- legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"),
- )
- .set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}"))
- .render("pie_scroll_legend.html")
- )
- print(hostname_dic)
這個(gè)是小編的歷史記錄
最后的最后這個(gè)小伙伴的女票一時(shí)忘記刪除歷史記錄,讓這個(gè)小腳本跑成功了,小伙伴也發(fā)現(xiàn)自己綠了。
總結(jié)
python 在 windows 上是可以干許多事情的,比如監(jiān)控屏幕發(fā)送到 QQ、微信等等,所以小伙伴們千萬(wàn)別三心二意哦。