1

I am using cookielib(cookiejar named now in Python3) and mechanize.

I am importing this way

import requests
from bs4 import BeautifulSoup
import mechanize
try:
    from cookielib import Cookie, CookieJar         
except ImportError:
    from http.cookiejar import Cookie, CookieJar as cookielib    

but I keep getting this error (AttributeError: type object 'CookieJar' has no attribute 'LWPCookieJar') at this point of my code

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

Any ideas?

Thanks a lot for your time!

limitcracker
  • 2,208
  • 3
  • 24
  • 23

1 Answers1

0

You can import http.cookiejar as cookielib when using python 3, they have almost the exact same interface.

try:
    import cookielib         
except ImportError:
    from http import cookiejar as cookielib

# Now you can use cookielib.LWPCookieJar no matter what version of python you're using
cookielib.LWPCookieJar()
cookielib.CookieJar()  # or the other classes
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50