Python: Getting all Urls from every open Google Ch

2020-07-06 06:15发布

I need to get all the urls from all open Google Chrome tabs in python 3 without intefering with the user. Im on Windows 10 using Microsoft Visual Studio Python3

Ive tried:

Opening it directly with open(path to current tabs)-- doesnt work because i have no permission- i think its locked because chrome activly writes 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 

PermissionError: [Errno 13] Permission denied

Opening through sglite3 -- doesnt work because its locked. And i cant find a password anywhere. Ive tried to open the History for the urls but it doesnt work anyways.

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

sqlite3.OperationalError: database is locked

Using selenium and a 3rd party chrome extension to copy all urls to the clipboard -- doesnt work because these extensions only work in the active selenium window. So the Windows with the tabs in it that i want dont get copied.

Ive considered hacking together a chrome extension that copys the urls every 30 sec to a temp file. But i only know minimal Javascript so this thing is driving me mad.

So does anyone know a way to do this in Python ? Any other solution is greatly appreciated.

1条回答
太酷不给撩
2楼-- · 2020-07-06 06:38

If you want to access the database, you should close all browsers.

(Source)

查看更多
登录 后发表回答