'Python 3.10 - error : 'cookielib' is not defined. Did you mean: 'CookieJar'?

Think I've been looking at this too long. But I'm trying to scrape a website using Python 3.10 Before I do that I need to login and this is my code:

import mechanize
from bs4 import BeautifulSoup
import urllib3

try:
    from http.cookiejar import CookieJar
except ImportError:
    from cookielib import CookieJar

cj = cookielib.CookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
br.open("https://mywebsite.com")

br.select_form(nr=0)
br.form['username'] = '[email protected]'
br.form['password'] = 'top_secret_password'
br.submit()

print (br.response().read())

I know that cookielib got replaced with cookiejar. When I run the below I keep receiving the error:

    cj = cookielib.CookieJar()
NameError: name 'cookielib' is not defined. Did you mean: 'CookieJar'?

where am I going wrong? I have a Try to import CookieJar

Any help would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source