Importing Cookies with Mechanize

576 Views Asked by At

(Using Python 2.7, with Mechanize)

Let's say I have a cookie on Twitter, named auth_token and it's Value is: ABC123.

https://i.gyazo.com/de568907c939617c14a874696664aeeb.png

How do tell Mechanize to import this Cookie? I've heard about Cookielib but I am not sure how to use it. I looked it up, but I've no clue how to set this up with Mechanize.

If someone could help me out, that would be awesome! :)

2

There are 2 best solutions below

1
Mike Tung On
import Cookie
import cookielib
cookiejar =cookielib.LWPCookieJar()

br = mechanize.Browser()
br.set_cookiejar(cookiejar)
cookie = cookielib.Cookie(auth_token='ABC123')
cookiejar.set_cookie(cookie)
0
Anunay On

Why not pass it as HTTP headers

br=mechanize.Browser()
br.addheaders = [('Cookie','name=value')]