Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - login a page and get cookies

First, thanks for taking the time to read this and maybe trying to help me.

I am trying to make a script to easily login in a site. I wanted to get the login cookies too, so maybe I could reuse them later. I made the script and it logs me in correctly. But I can not get the cookies. When I try to print them, I see just this:

<RequestsCookieJar[]>

Obviously this can't help me, I think. So now I would be interested in knowing how to get the real cookie data. Thanks a lot to whoever can hep me reaching that.

My code:

import requests
import cfscrape
from bs4 import BeautifulSoup as bs
header = {"User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
s = requests.session()
scraper=cfscrape.create_scraper(sess=s) #i use cfscrape because the page uses cloudflare anti ddos page
        scraper.get("https://www.bstn.com/einloggen", headers=header)
        myacc={"login[email]": "[email protected]", #obviously change
        "login[password]": "password123"}
        entry=scraper.post("https://www.bstn.com/einloggen", headers=header, data=myacc)
        soup=bs(entry.text, 'lxml')
        accnm=soup.find('meta',{'property':'og:title'})['content']
        print("Logged in as: " + accnm)
        aaaa=scraper.get("https://www.bstn.com/kundenkonto", headers=header)
        print(aaaa.cookies)

If I print the ccokies, I just get the <RequestsCookiesJar[]> like described earlier... It would be really nice if anyone could help me getting the "real" cookies

like image 401
rocket Avatar asked Nov 25 '25 08:11

rocket


1 Answers

If you want to get your login cookie that you ought to use the response which after posting, because you are doing login action! Server will send back session cookies if you input correct email&password. And why you got empty cookies in aaaa is website didn't want to set or change your cookies.

entry = scraper.post("https://www.bstn.com/einloggen", allow_redirects=False, headers=header, data=myacc)
print(entry.cookies)
like image 64
KC. Avatar answered Nov 26 '25 23:11

KC.