使用Python删除 Google 浏览器历史记录
在本文中,您将学习编写一个Python程序,该程序将用户输入作为关键字,如 Facebook、amazon、geeksforgeeks、Flipkart、youtube 等,然后在您的 google chrome 浏览器历史记录中搜索该关键字以及该关键字是否在任何 URL 中找到,然后将其删除。例如,假设您输入了关键字“geeksforgeeks”,那么它会搜索您的 google chrome 历史记录,例如“www.geekforgeeks.org”,很明显该 URL 包含关键字“geeksforgeeks”,然后将其删除,它还将搜索标题中包含“geeksforgeeks”的文章(例如“geeksforgeeks 是准备竞争性编程面试的好门户吗?”)并将其删除。首先,获取系统中 google chrome 历史文件所在的位置。
注意: Windows 中的 Google chrome 历史文件位置一般为:C:\Users\manishkc\AppData\Local\Google\Chrome\User Data\Default\History。
执行:
import sqlite3
# establish the connection with
# history database file which is
# located at given location
# you can search in your system
# for that location and provide
# the path here
conn = sqlite3.connect("/path/to/History")
# point out at the cursor
c = conn.cursor()
# create a variable id
# and assign 0 initially
id = 0
# create a variable result
# initially as True, it will
# be used to run while loop
result = True
# create a while loop and put
# result as our condition
while result:
result = False
# a list which is empty at first,
# this is where all the urls will
# be stored
ids = []
# we will go through our database and
# search for the given keyword
for rows in c.execute("SELECT id,url FROM urls\
WHERE url LIKE '%geeksforgeeks%'"):
# this is just to check all
# the urls that are being deleted
print(rows)
# we are first selecting the id
id = rows[0]
# append in ids which was initially
# empty with the id of the selected url
ids.append((id,))
# execute many command which is delete
# from urls (this is the table)
# where id is ids (list having all the urls)
c.executemany('DELETE from urls WHERE id = ?',ids)
# commit the changes
conn.commit()
# close the connection
conn.close()
输出:
(16886, 'https://www.geeksforgeeks.org/')