gpt4 book ai didi

Python - 成功使用 Cookie

转载 作者:行者123 更新时间:2023-11-28 17:39:51 25 4
gpt4 key购买 nike

当我尝试使用 facebook cookies 在 facebook 上打开一个登录帐户时......

import urllib2, urllib, cookielib 

jar = cookielib.CookieJar()
cookie = urllib2.HTTPCookieProcessor(jar)
opener = urllib2.build_opener(cookie)

data = urllib.urlencode({'email':'user@email.com','pass':'swagpassword','login':'Log+In'})
req = urllib2.Request('http://www.facebook.com/login.php')
response = opener.open(req, data)
cookie_header = response.headers.get("Set-Cookie")
response = opener.open(req, data) #I open it twice on purpose

if "Logout" in response.read():
print("Logged In")


jar = cookielib.CookieJar() #new instance
cookie = urllib2.HTTPCookieProcessor(jar) #new instance
opener = urllib2.build_opener(cookie) #new instance

cookie_request = urllib2.Request('http://www.facebook.com/login.php')
cookie_request.add_header("cookie", cookie_header)

cookie_POST = opener.open(cookie_request)
cookie_POST = opener.open(cookie_request)

if "Logout" in cookie_POST.read():
print("Logged In")

它会在第一次成功打印“已登录”,但是当我尝试使用 cookie 时,我不会登录。我该如何解决这个问题? (不使用其他下载的模块)

最佳答案

只需重用旧实例。

....

if "Logout" in response.read():
print("Logged In")

#jar = cookielib.CookieJar() #new instance
#cookie = urllib2.HTTPCookieProcessor(jar) #new instance
#opener = urllib2.build_opener(cookie) #new instance

cookie_request = urllib2.Request('http://www.facebook.com/login.php')
#cookie_request.add_header("cookie", cookie_header)

cookie_POST = opener.open(cookie_request)

...

关于Python - 成功使用 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25956080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com