[Python tips] browser_cookie3 access Chrome browser Cookies file error solution


foreword

Recently, I need to use Chrome to visit the webpage, and need to extract its cookie through browser_cookie3 as login verification. The code written in the previous 113 version of the Chrome browser can run normally, but recently there are frequent errors and it cannot run normally. It's a headache.


1. Browser_cookie3 reports an error when accessing the Chrome browser Cookies file

  1. The error message is as follows, the file access is denied, and the file path is found to be wrong
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 1165, in chrome
    return Chrome(cookie_file, domain_name, key_file).load()
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 494, in load
    with _DatabaseConnetion(self.cookie_file) as con:
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 352, in __enter__
    return self.get_connection()
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 388, in get_connection
    con = method()
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 379, in __get_connection_legacy
    shutil.copyfile(self.__database_file, self.__temp_cookie_file)
  File "D:\ProgramData\anaconda3\lib\shutil.py", line 254, in copyfile
    with open(src, 'rb') as fsrc:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Administrator\\AppData\\Roaming\\..\\Local\\Google\\Chrome\\User Data\\Default\\Network\\Cookies'

After research, the library file __init__.py of browser_cookie3 put the following codes in the third order

            genararated_paths.append(
                {
    
    'env': 'APPDATA', 'path': '..\\Local\\' + path.format(channel=chan)})

It ends up as follows:

def _genarate_win_paths_chromium(paths: Union[str, list], channel: Union[str, list] = None):
    """Generate paths for chromium based browsers on windows"""

    paths, channel = _normalize_genarate_paths_chromium(paths, channel)
    genararated_paths = []
    for chan in channel:
        for path in paths:
            # genararated_paths.append(
                # {'env': 'APPDATA', 'path': '..\\Local\\' + path.format(channel=chan)})
            genararated_paths.append(
                {
    
    'env': 'LOCALAPPDATA', 'path': path.format(channel=chan)})
            genararated_paths.append(
                {
    
    'env': 'APPDATA', 'path': path.format(channel=chan)})
            genararated_paths.append(
                {
    
    'env': 'APPDATA', 'path': '..\\Local\\' + path.format(channel=chan)})
    # print(genararated_paths)
    return genararated_paths

The path is correct, but it still prompts that access is denied!

File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 1237, in load
    for cookie in cookie_fn(domain_name=domain_name):
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 1164, in chrome
    return Chrome(cookie_file, domain_name, key_file).load()
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 493, in load
    with _DatabaseConnetion(self.cookie_file) as con:
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 351, in __enter__
    return self.get_connection()
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 387, in get_connection
    con = method()
  File "D:\ProgramData\anaconda3\lib\site-packages\browser_cookie3\__init__.py", line 378, in __get_connection_legacy
    shutil.copyfile(self.__database_file, self.__temp_cookie_file)
  File "D:\ProgramData\anaconda3\lib\shutil.py", line 254, in copyfile
    with open(src, 'rb') as fsrc:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Network\\Cookies'
  1. Try to update the Chrome browser (the earliest version was version 113, I don’t know when it was updated to version 114), the problem remains;
  2. Replace the browser_cookie3 library version, install the latest version, and change to the old version, the problem remains;
  3. Using sqlite to access directly, it also cannot be opened, and the error is as follows:
con = sqlite3.connect(filename)
sqlite3.OperationalError: unable to open database file

Tried many methods and modified a lot of codes, but the problem still persists.

2. There is no place to find when you break through the iron shoes, and it takes no effort to get it

After many days of research, it was found that there was a problem when shutil.copyfile copied the file (excluding the error reported on the Internet because the target address is a directory and not the file name), because the Cookies file was opened by the Chrome browser, and the copy cannot be completed in the occupied state . Of course, it is not allowed to open directly, so sqlite cannot read cookies.

Since the Chrome browser is automatically updated to version 114, the Cookies file is opened and occupied, making it impossible to access it through other methods.

Once the problem is found, the solution becomes clear.

1. Change to the old version (version 113) of the Chrome browser. But you must disable automatic updates, otherwise the problem will remain after the upgrade.
2. Switch the access code to other browsers supported by browser_cookie3.

Here the author changed to Microsoft Edge browser, and the code is modified as follows:

    #cookies = browser_cookie3.chrome(domain_name='xxxx')
    cookies = browser_cookie3.edge(domain_name='xxxx')

So far, the problem is solved.

Finally, update the library to the latest version through pip install browser_cookie3 -U, and the access is normal.

(base) C:\Users\Administrator>pip install browser_cookie3 -U
Requirement already satisfied: browser_cookie3 in d:\programdata\anaconda3\lib\site-packages (0.19.0)
Collecting browser_cookie3
  Using cached browser_cookie3-0.19.1-py3-none-any.whl (14 kB)
Requirement already satisfied: pycryptodomex in d:\programdata\anaconda3\lib\site-packages (from browser_cookie3) (3.17)
Requirement already satisfied: lz4 in d:\programdata\anaconda3\lib\site-packages (from browser_cookie3) (3.1.3)
Installing collected packages: browser_cookie3
  Attempting uninstall: browser_cookie3
    Found existing installation: browser-cookie3 0.19.0
    Uninstalling browser-cookie3-0.19.0:
      Successfully uninstalled browser-cookie3-0.19.0
Successfully installed browser_cookie3-0.19.1

Summarize

Although the solution is very simple, I have experienced various twists and turns in the process, but I also have a deeper understanding of browsers and operating systems.
Although there are some problems in program debugging, it also proves that Chrome browser is stronger in terms of security.

In addition, solving problems is the same as solving equations. If one road is blocked, you can change another road. Maybe the problem can be solved soon!

Although debugging is sometimes annoying, it is also one of the joys of programming!

Guess you like

Origin blog.csdn.net/popboy29/article/details/131416800