Python-如何保存/加载特定cookie?

Python-如何保存/加载特定cookie?,python,cookies,save,load,roblox,Python,Cookies,Save,Load,Roblox,我有以下Python代码,应该使用.ROBLOSECURITY cookie登录网站。它还包括一个除IOERROR:之外的函数,因此如果.ROBLOSECURITY cookie未登录,它将使用用户名/密码登录,并保存从中获取的cookie import urllib2 import urllib import cookielib try: cookielib.LWPCookieJar().load("cookies.txt") #Trying to load the cookie f

我有以下Python代码,应该使用.ROBLOSECURITY cookie登录网站。它还包括一个除IOERROR:之外的
函数,因此如果.ROBLOSECURITY cookie未登录,它将使用用户名/密码登录,并保存从中获取的cookie

import urllib2
import urllib
import cookielib

try:
    cookielib.LWPCookieJar().load("cookies.txt") #Trying to load the cookie file

except IOError: #In case the cookies.txt fails to log in. I don't know if IOError is the correct error specification for an expired cookie
    print "Loading stored cookies failed, manually logging in..."
    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    urllib2.install_opener(opener)
    authentication_url = 'https://www.roblox.com/newlogin'
    payload = {
        'username' : 'UsernameHere',
        'password' : 'PasswordHere',
        '' : 'Log In',
        }
    data = urllib.urlencode(payload)
    req = urllib2.Request(authentication_url, data)
    resp = urllib2.urlopen(req)
    cj.save("cookies.txt") #Saving to the cookie file

tc = urllib2.urlopen("http://www.roblox.com/My/Money.aspx").read() #The hidden page
checksession = re.findall('My Transactions',tc) #Just checking for text that is only found on the hidden page
print checksession
我认为
cookielib.LWPCookieJar().load(“cookies.txt”)
不起作用,因为它也在加载除.ROBLOSECURITY之外的其他cookie(我知道如果您只使用它,它会登录)。如何仅加载.ROBLOSECURITY cookie或仅保存.ROBLOSECURITY(以便其他cookie不会干扰.ROBLOSECURITY登录)

另外,我不确定我的
除了IOError:
是否能正常工作,因为我只知道如果我将
cookielib.LWPCookieJar().load(“cookies.txt”)
更改为
cookielib.MozillaCookieJar().load(“cookies.txt”)


最后,我如何将.ROBLOSECURITY的过期日期更改为2050-12-31 24:00:00Z,这是我的最终代码:

import urllib #Idk if this is necessary, but I'm not gonna bother checking. Only time I used it was for urllib.urlencode, which idk if urllib2 can do
import urllib2
import cookiejar
try:
    cj = cookielib.MozillaCookieJar("./cookies.txt")
    cj.load()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    tc = opener.open("http://www.roblox.com/My/Money.aspx").read()

#    print "Loading stored cookies succeeded, automatically logging in..."

#    checksession = re.findall('My Transactions',tc)

#    if len(checksession) > 0:
#        print "Login success!"

except IOError:
    print "Loading stored cookies failed, manually logging in..."
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    urllib2.install_opener(opener)
    authentication_url = 'https://www.roblox.com/newlogin'
    payload = {
        'username' : 'USERNAMEHERE',
        'password' : 'PASSWORDHERE',
        '' : 'Log In', #In hindsight Idk if this is necessary, but I don't feel like checking now
        }
    data = urllib.urlencode(payload)
    req = urllib2.Request(authentication_url, data)
    resp = urllib2.urlopen(req)
    cj.save("./cookies.txt") #./ to make sure its in the right directory

    tc = opener.open("http://www.roblox.com/My/Money.aspx").read() #Only accessible if logged in
#    checksession = re.findall('My Transactions',tc) #The rest is to check if log in was success by looking for specific phrase in the page

#    if len(checksession) > 0:
#        print "Login success!"

我自己解决了这个问题。如果你想知道我的源代码,让我知道。是的,请张贴源代码,因为我有同样的问题我有一个最新的源代码,虽然我发现使用请求更容易。我会发布一个答案。