python webkit webview remember cookies?

后端 未结 2 1787
孤独总比滥情好
孤独总比滥情好 2020-12-06 08:23

I have written a short python script that opens Google music in web view window. however I can\'t seem to find anything about getting webkit to use cookies so that I don\'t

相关标签:
2条回答
  • 2020-12-06 08:49

    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.

    This is basically same answer from Matt, just using GIR introspection and feels more pythonish.

    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)
    
    0 讨论(0)
  • 2020-12-06 09:10

    Worked it out, but it required learning more ctypes than I wanted -_-. Try this- I required different library paths, etc than you, so I'll just paste what's relevant.

    #remove all cookiejars
    generic_cookiejar_type = libgobject.g_type_from_name('SoupCookieJar')
    libsoup.soup_session_remove_feature_by_type(session, generic_cookiejar_type)
    
    #and replace with a new persistent jar
    cookiejar = libsoup.soup_cookie_jar_text_new('/path/to/your/cookies.txt',False)
    libsoup.soup_session_add_feature(session, cookiejar)
    

    The code's pretty self explanatory. There's also a SoupCookieJarSqlite that you might prefer, though I'm sure the text file would be easier for development.

    EDIT: actually, the cookie jar removal doesn't seem to be doing anything, so the appropriate snippet is

    #add a new persistent cookie jar
    cookiejar = libsoup.soup_cookie_jar_text_new('/path/to/your/cookies.txt',False)
    libsoup.soup_session_add_feature(session, cookiejar)
    
    0 讨论(0)
提交回复
热议问题