Reputation: 141
I need to get all urls from all open Google Chrome tabs using python 3 without intefering with the user. My operating system is Windows 10 and I use Microsoft Visual Studio Python3.
I have tried the following things:
Opening the tabs directly using open(path_to_current_tabs)
. This doesn't work because I don't have permission to read the file. I think that it's locked because chrome is actively writing to it.
Current_Tabs_Source = open(r"C:\Users\Beni\AppData\Local\Google\Chrome\User Data\Default\Current Tabs", "r")
Current_Tabs_Raw = Current_Tabs_Source.read()
print(Current_Tabs_Raw) #just for checking
The resulting error:
PermissionError: [Errno 13] Permission denied
Opening using the history
SQlite3 database also doesn't work because it's locked. But I can't find a password anywhere. I have also tried to open the History for the urls, but that didn't work either.
import sqlite3
from os import path
data_path = path.expanduser('~') + r"\AppData\Local\Google\Chrome\User
Data\Default"
files = listdir(data_path)
history_db = path.join(data_path, 'history')
c = sqlite3.connect(history_db)
cursor = c.cursor()
select_statement = "SELECT urls.url, urls.visit_count FROM urls, visits
WHERE urls.id = visits.url;"
cursor.execute(select_statement)
results = cursor.fetchall()
print(results) #just for checking
This results in the following error:
sqlite3.OperationalError: database is locked
Using selenium and a third party chrome extension to copy all urls to the clipboard didn't work either, because these extensions only work in the active selenium window. So the links of the tabs that I want don't get copied.
I have considered hacking together a chrome extension that copies the urls every 30 sec to a temp file, but my javascript knowledge is minimal, so that thing is driving me mad.
Does anyone know a way to do this in Python? Any other solution would be greatly appreciated.
Upvotes: 11
Views: 5903
Reputation: 1
You can use windows shadow copy service to make copy of locked sqlite database. And then read it normally.
There is python module for that https://github.com/sblosser/pyshadowcopy
Upvotes: 0
Reputation: 1108
When we're accessing the SQLite
database of any browser, we have to close that particular browser first.
Moreover, the SQL command being used here will fetch all the duplicate rows.
Need to change `select_statement'
select_statement = "SELECT distinct urls.url, urls.visit_count FROM urls, visits WHERE urls.id = visits.url;"
Further, we need to add a loop to print all the 'urls' from history database of chrome.
for url, count in results:
print(url) # print urls line by line
However, this will give us the whole history of the Chrome browser but not the required URLs of all the presently opened tabs.
Upvotes: 1
Reputation: 65
If you want to access the database, you should close all browsers.
(Source)
Upvotes: 1