当库不能为您处理请求时,如何从请求中获取特定的cookie

2024-09-27 22:31:23 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有这个页面,我需要不断登录,但我甚至不能登录的网页,如果我不使用一些cookies在标题。你知道吗

问题是,如果我使用邮差给我的cookies,它们最终会过期,我必须在代码中再次替换它们。你知道吗

    #####this makes the login

url = "https://www.APAGE.com"#login url

payload = "user="+dude["user"]+"&password="+dude["password"]+"&action=login"
headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0", #for some reason i need this or the page returns error
'Content-Type': "application/x-www-form-urlencoded",
'X-Requested-With': "XMLHttpRequest",
'Cookie': "ASP.NET_SessionId=<an usually expirable cookie>; __RequestVerificationToken_L0NvbnN1bHRhV2Vi0=<another expirable cookie>",#i need THESE!
'Cache-Control': "no-cache",
}


login = session.post(url, data=payload, headers=headers)# makes the login
print "Login open"
cookie = session.cookies.get_dict() #get the recursive cookie

             #this here is me trying to grab the request-cookies just after the login so i can repass them so they don't expire

print '================'
print login.request.headers
print '================'
print '\n\n\n'
cookie2 = login.headers.get('Set-Cookie')
print login.headers
print cookie2
print login.cookies.get_dict()


#makes a get request to change to the initial page

url = "www.APAGE-after-login.com"

headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0",
'Referer': "www.APAGE-after-login.com",
'Cookie': "ASP.NET_SessionId=<the cookie again>.; __RequestVerificationToken_L0NvbnN1bHRhV2Vi0=<the other cookie>; .ASPXAUTH="+str(cookie['.ASPXAUTH']), #here i need to repost the .ASPAUTH cookie every time after a request or the session expires
'Upgrade-Insecure-Requests': "1",
'Cache-Control': "no-cache",
}


moving = session.get(url,headers=headers)

cookie = session.cookies.get_dict()

我需要这里的帮助来获取这些cookies,这样,当它们更改时,我就不必一次又一次地更改整个代码部分。 有人知道我如何拦截那些请求cookies以便我使用它们吗? 谢谢!你知道吗

编辑:我已经有会话=请求.会话()在代码中声明,并且我已经尝试了几种解决问题的方法。。。如果我手动将cookie放在头上,代码就会工作,但是cookie将在几天后过期。。。由于某些原因,请求库无法自动处理Cookie。。。你知道吗

如果我使用此标题:

    headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0",
'Cookie': .ASPXAUTH="+str(cookie['.ASPXAUTH']),
'Cache-Control': "no-cache",
}

或任何其他变化

moving = session.get(url,headers=headers,cookies=cookie) #cookie that i tried to get before

登录根本不起作用。它返回一个错误页。你知道吗

谢谢

编辑2:

for customer in customers:
session = requests.session()



##create a folder
if not os.path.exists("C:\\Users\\Desktop\\customers\\" + customer["dir"] + "/page"):
    os.makedirs("C:\\Users\\Desktop\\customers\\" + customer["dir"] + "/page", 0755)

search_date= datetime.datetime.now().strftime("%d-%m-%Y-%H-%M-%S")
search_date_end=  (datetime.datetime.now() - timedelta(days = 30)).strftime("%d/%m/%Y") 
search_date_begining= (datetime.datetime.now() - timedelta(days = 30)).strftime("%d/%m/%Y")


search_date_closing= (datetime.datetime.now() - timedelta(days = 45)).strftime("%d/%m/%Y") 
search_date_closing= urllib.quote_plus(data_busca_fechamento)
search_date_begining= urllib.quote_plus(data_busca_inicio)
search_date_end= urllib.quote_plus(data_busca_fim)


print str(search_date_end)


######makes the login

url = "www.ASITE.com/aunthenticate/APAGELogin" #login
payload = "user="+customer["user"]+"&password="+customer["pass"]+"&action=login"
headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0", #for some reason i need this or the login breaks 
'Content-Type': "application/x-www-form-urlencoded",
'X-Requested-With': "XMLHttpRequest",
'Cookie': "ASP.NET_SessionId=<some cookie>;  __RequestVerificationToken_L0NvbnN1bHRhV2Vi0=<part1cookie>-<part2cookie>-<part3cookie>", #i need to get these cookies to login, for some reason i cant get them by any means
'Cache-Control': "no-cache",
}



login = session.post(url, data=payload, headers=headers)#open the login session on the page
print "Login session open"
cookie = session.cookies.get_dict() #when i get this cookie i only get the recursive cookie '.ASPXAUTH' that i need to get again every request or the session expires
print login.text
#The response has only one line with some site data confirming the login
#if the login fails it returns an HTML with the error message


#here i try to get the request cookies and not the response ones, but the headers dont return any cookies at all
print '================'
print login.request.headers
print '================'
print '\n\n\n'
cookie2 = login.headers.get('Set-Cookie')
print login.headers
print cookie2
print login.cookies.get_dict() #this cookie is returned, but just the '.ASPXAUTH' one, the one i already know how to get





#makes the get request to the index page
url = "www.ASITE/index/home"

headers = {
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0", #again i need to pass the user-agent
'Accept-Language': "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
'Cookie': "ASP.NET_SessionId=<a cookie>; __RequestVerificationToken_L0NvbnN1bHRhV2Vi0=<other long cookie>; .ASPXAUTH="+str(cookie['.ASPXAUTH']), #here i need to start passing the recursive cookie again and again every request on the site
'Upgrade-Insecure-Requests': "1",
'Cache-Control': "no-cache",
}


moving = session.get(url,headers=headers)

cookie = session.cookies.get_dict() #get the '.ASPXAUTH' again

这里的问题是,如果我手动设置丢失的cookie,代码将在几天内工作,但是当它们过期或其他机器使用代码时,我必须再次手动设置它们。 通过这种方式,我尝试了几种方法在请求之前获取另外两个cookie,但没有一个真正起作用,而且,由于某种原因,“请求”库没有像它应该的那样自动处理它们。。。我真的不知道该怎么办了。你知道吗


Tags: thetourlsearchgetcookierequestsession
1条回答
网友
1楼 · 发布于 2024-09-27 22:31:23

代码开始工作了。好消息是现在的代码正在以正确的方式获取cookies;坏消息是我完全不知道这是怎么发生的。你知道吗

我唯一添加的是这段代码:(关键是我昨天添加了它,然后它就不起作用了。。。现在可以了)。你知道吗

headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0",
'Content-Type': "application/x-www-form-urlencoded",
'X-Requested-With': "XMLHttpRequest",
'Cache-Control': "no-cache",
}



url = "www.asite.com/login" #login page

login = session.get(url, headers=headers) #login get




print 'login.request.headers================'
print login.request.headers
print '================'
print '\n\n\n'
cookie2 = login.headers.get('Set-Cookie')
print 'login headers ============================='
print login.headers
headers = login.headers
print '\n\n\n'
print 'login.headers.get(''Set-Cookie'') ================================'
print cookie2
print '\n\n\n'
print "login.cookies.get_dict() ========================="
test = login.cookies.get_dict()
print test
print '\n\n\n'

昨天login.cookies.get\u dict()只返回空dict或none,或者,如果放在登录之后,则只返回递归cookie。。。现在。。。它正在工作。你知道吗

相关问题 更多 >

    热门问题