webkit: is it possible to store cookies to file an

2019-05-10 05:58发布

问题:

is it possible to store cookies to file when you use webkit and reuse it again next time when I run my application?

回答1:

I know its old question and have been looking for the answer all over the place. Finally came up on my own after some trial and error. Hope this helps others.

from gi.repository import Soup
cookiejar = Soup.CookieJarText.new("<Your cookie path>", False)
cookiejar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS)
session = WebKit.get_default_session()
session.add_feature(cookiejar)


回答2:

For those using WebKit and Gtk, change the first line from:

from gi.repository import Soup

to:

from gi.repository import Gtk, WebKit, Soup

to avoid static and dynamic conflicts.