0

I'm trying to login to a website (tesco.com) and get the HTML of a secure page.

My plan was to use the requests library and create a session... then use that session to go to the secure page (of orders).

I think my problem is that I'm not clicking 'Submit' but its a button not a normal submit... Hoping for some help please!

I'm new to Python.

import requests
EMAIL = 'MYUSERNAME@EMAIL.com'
PASSWORD = 'MYPASSWORD123!'

URL = 'https://secure.tesco.com/account/en-GB/login'
session = requests.session()
login_data = {
    'email': EMAIL,
    'password': PASSWORD
}
r = session.post(URL, data=login_data) 
r = session.get('https://www.tesco.com/groceries/en-GB/orders/449555448_6781')
print(r).text

I'm hoping to authenticate so I can use session.get to retrieve my orders

In Developer tools here is the actual request:

Request URL: https://secure.tesco.com/account/en-GB/login? 
from=https%3A%2F%2Fwww.tesco.com%2Fgroceries%2Fen-GB%2F
Request Method: POST
Status Code: 302 Moved Temporarily
Remote Address: 104.103.115.230:443
Referrer Policy: no-referrer-when-downgrade
Accept: text/html,application/xhtml+xml,application/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 141
Content-Type: application/x-www-form-urlencoded
Cookie: 

mytesco_segment_register=%7B%22segment%22%3A%22withMerge%22%2C%22;
mytesco_segment_forgottenPassword=%7B%22segment%22%3A%22new%22%2C%22;; 
mytesco_segment_notification=%7B%22segment%22%3A%22;
cookiesAccepted=1; 
mytesco_from=https%3A%2F%2Fwww.tesco.com%2Fgroceries%2Fen-GB%2F; 
cookiesAccepted=1546725466977; _csrf=dQoI2MTOmJKjhD4ycImJVQiF; 
akavpau_tesco_secure=1546725768~id=a6872943921fff98c4c7a240506d1766
Host: secure.tesco.com
Origin: https://secure.tesco.com
Pragma: no-cache
Referer: https://secure.tesco.com/account/en-GB/login? 
from=https%3A%2F%2Fwww.tesco.com%2Fgroceries%2Fen-GB%2F
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Mobile 
Safari/537.36
from: https://www.tesco.com/groceries/en-GB/
username: *****
password: *****
state: 35c4c1b2-897c-40a9-9103-3b3b66e2a5b0
_csrf: zzS9vS3P-gw9jUzOV-jkELiZ-Yi-EBfUSg1E

I get back the login page (almost as if the click hasn't happened).

  • If you look at your browser's developer tools and take a note of what request it makes when you login via the site itself, use that and then emulate that - buttons/such shouldn't matter much. – Jon Clements Jan 05 '19 at 22:39
  • Added more info to the post with whats sent, but i wonder if i'm not actually 'clicking' to login... – Toby Lockyer Jan 05 '19 at 22:49
  • Trying doing a `session.get(URL)` first to make sure you've got the cookies and other state data so that the post request can include that – Jon Clements Jan 05 '19 at 23:02

1 Answers1

0

Given that you want to interact with a button, that task may be a better fit for Selenium https://selenium-python.readthedocs.io/ instead of just requests

Jorge Lavín
  • 937
  • 8
  • 22
  • I just want to click one button though, a login button. Selenium seems over complicated for just this task, and also can it run silently? when i've tried, it actually launches Chrome / Firefox :) – Toby Lockyer Jan 05 '19 at 22:50
  • You may use ghost driver https://stackoverflow.com/questions/33074133/what-is-the-difference-between-selenium-webdriver-and-selenium-ghostdriver – Jorge Lavín Jan 05 '19 at 22:51