Экспорт файлов cookie сеанса в формат JSON для импорта в Chrome - PullRequest
1 голос
/ 07 февраля 2020

Я пытаюсь экспортировать куки из сеанса, но используемый формат не соответствует моим потребностям. Мне нужно импортировать эти файлы cookie в драйвер Selenium chrome.

import requests
from bs4 import BeautifulSoup


base = "https://amazon.com/ap/signin?ie=UTF8&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_ya_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&switch_account=signin&ignoreAuthState=1&disableLoginPrepopulate=1&ref_=ap_sw_aa"
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Origin": "https://amazon.com/ap/signin", "Referer": "https://amazon.com/ap/signin", "Connection": "close"}
head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Content-Type": "application/x-www-form-urlencoded", "Origin": "https://amazon.com", "Connection": "close"}

users = {}

session = requests.session()
response = session.get(base, headers=header)
soup = BeautifulSoup(response.text, "html.parser")

openid = soup.find('input', {'name':'openid.return_to'})['value']
prevRID = soup.find('input', {'name':'prevRID'})['value']
workflowState = soup.find('input', {'name':'workflowState'})['value']
appActionToken = soup.find('input', {'name':'appActionToken'})['value']
session_id = session.cookies['session-id']

email = "email"
pw = "password"

data = {"appActionToken": appActionToken, "appAction": "SIGNIN", "openid.return_to": openid, "prevRID": prevRID,
        "workflowState": workflowState, "email": email, "create":0, "password": pw}

response = session.post("https://amazon.com/ap/signin", data=data, headers=head)
main_page = session.get("https://www.amazon.com/?ref_=nav_ya_signin&", headers=head)

if "Welcome to Amazon" in main_page.text:
    # export cookies
    print("Exporting cookies..")


else:
    # sign in failed.
    print("Fail")
...