简体   繁体   中英

How to fix AttributeError: type object 'CookieJar' has no attribute 'LWPCookieJar'?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM