【发布时间】:2015-05-24 09:20:44
【问题描述】:
我有以下 Python 代码,应该使用 .ROBLOSECURITY cookie 登录网站。它还包括一个except 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 登录)?
另外,我不确定我的except IOError: 是否能正常工作,因为我只知道将cookielib.LWPCookieJar().load("cookies.txt") 更改为cookielib.MozillaCookieJar().load("cookies.txt") 才能正常工作
最后,如何将 .ROBLSECURITY 的到期日期更改为 2050-12-31 24:00:00Z 之类的日期
【问题讨论】:
-
我自己解决了这个问题。如果您想知道我的有效源代码,请告诉我。
-
是的,请发布源代码,因为我也有同样的问题
-
我有一个最新的源代码,虽然我发现使用请求更容易。我会发布一个答案。
标签: python cookies save load roblox