2

I want to sign in Amazon.com with Python. But I can't do it even if I use requests.Session() , because I can't "Enable Cookies".

Could you tell me how to fix the code? Why "dict(response.cookies)" return empty?

    # creating all input tag information (hidden tags are included)
    # {'pageId': 'ape:dXNmbGV....'email': 'xxx@xxx', 'password': 'xxx', 'create': '0'}
    def create_signin_data(res, user, pass) -> str:
        bs = BeautifulSoup(res.content, "html.parser")
        signin_data = {s["name"]: s["value"]
                       for s in bs.select("form[name=signIn]")[0].select("input[name]")
                       if s.has_attr("value")}
        signin_data[u"email"] = user
        signin_data[u"password"] = pass
        return signin_data

    signin_url ="https://www.amazon.com/ap/signin?_encoding=UTF8&........."
    action_url ="https://www.amazon.com/ap/signin"

    ### create session
    session = requests.Session()
    res = session.get(signin_url)
    # res = session.get(signin_url, cookies = res.cookies) -> the result is the same
    cookie_data = dict(response.cookies) # empty dict {}

    ### sign in
    signin_data = create_signin_data(res, "user@addr", "pass") 
    res = session.post(signin_url, signin_data)
    # res = session.post(action_url, signin_data) -> the result is the same
    # res = session.post(signin_url, signin_data, cookies=cookie_data ) -> the result is the same

    print(res.content)

the last out put (html) -----------------
Please Enable Cookies to Continue To continue shopping at Amazon.com, please enable cookies in your Web browser. Once you have enabled cookies in your browser, please click on the button below to return to the previous page.

I want to get post-login top page (your Amazon.com)

mon
  • 21
  • 1
  • 3
  • 1
    why do you need to get signin url ? If you inspect the login form in amazon.com, you will find that form has action and method. And, use the inputs data to submit the form. – Sachin Nov 04 '17 at 08:29
  • signin url and action value is almost same. if I run "session.post(action_url, signin_data)", the result html says "Please Enable Cookies" – mon Nov 04 '17 at 13:00

3 Answers3

0

Your usage of requests.Session() is proper, so I'd reckon that the problem in not with cookies but rather with your login. Websites like Amazon and Facebook sometimes require form metadata along with login web requests that you are currently not passing. If I am right, this answer should be able to help you.

emporerblk
  • 1,063
  • 5
  • 20
0

Finally I can't find the answer... Instead I use "Selenium" and I can do it easily.

driver = Chrome(DRIVER_PATH)
driver.get(TOP_URL)
driver.find_element_by_id(SIGNIN_BUTTON).click()
driver.find_element_by_id(MAIL).send_keys(user_name)
elem_pass = driver.find_element_by_id(PASSWORD)
elem_pass.send_keys(user_password)
elem_pass.submit()
mon
  • 21
  • 1
  • 3
-1
res = s.get('https://mysite[dot]com')
cookies = dict(res.cookies)
res = s.post('https://login[dot]mysite[dot]com', auth=('Email', 'Password'), verify=False, 
cookies=cookies)

you can pass cookies along post() call.

Mahesh Karia
  • 2,045
  • 1
  • 12
  • 23