R
R
R4ndolphC4rter2019-07-12 12:50:17
Python
R4ndolphC4rter, 2019-07-12 12:50:17

Python Requests How to login to a website?

Good afternoon!
My goal is to
write a program in python. The login and password data are given at the input, the True/False response is given at the output (depending, respectively, on whether the program was able to log in).
Authorization takes place on the Netflix website, this is my task.
Implementation 1:
To begin with, I decided to write code, the result of which was print(r.text). As I understand it, when entering the correct and incorrect login and password, the results ( print(r.text) ) should be different, but they are the same for me.
КОД УДАЛЁН
Implementation 2:
Result (with valid/invalid input):
' https://www.netflix.com/Login?nextpage=https%3A%2F... '
Expected result (with valid input):
'https://www.netflix.com/browse '
КОД УДАЛЁН
07/13/19
The 3rd day of my torment has begun, I keep you informed ...
19:55, I returned from school, I continue my futile attempts.
My plans? I'm going to send a post request and then send a get request trying to access https://www.netflix.com/browse (a page that can't be accessed without logging in).

r1 = requests.post(URL_LOG, data=data, headers=headers)

I'm trying to delve into other people's codes, where they get the data for the dictionary from - I don't know. (Where did I get the data for the headers dictionary? I copied it from someone else's code (and there were several of them), alas, they no longer work). Some sort of secret investigation. I'm going to read literature, look, I'll get into it from the hundredth time.
After numerous requests to the server, Netflix temporarily blocked my ip. Download VPN to computer. Where I haven't been today! Canada, Germany, USA, France, Norway and many other places. I continue to suffer ... Because my get request at https://www.netflix.com/browse returns url = https://www.netflix.com/login (as if I was not logged in, but the login and password are then true!).
Current code (13.07.19 21:14) aka Implementation 3:
import requests
from lxml import html, etree

URL_LOG = "https://www.netflix.com/login"
URL_MAIN = "https://www.netflix.com/browse"
LOGIN = "MY_LOGIN"
PASSWORD = "MY_PASSWORD"

s = requests.Session()

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
    "Connection": "keep-alive",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
}

result = requests.get(URL_LOG, headers=headers)
tree = html.fromstring(result.text)
authURL = list(set(tree.xpath("//input[@name='authURL']/@value")))[0]

print(authURL)

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive",
    "Content-Length": "313",
    "Content-Type": "application/x-www-form-urlencoded",
    #"Cookie": "nfvdid=BQFnAAEBEJxbF-N0cMOnfTmUxgT9zsZA_s_RaJ0fg2fV7bXcmlV1-6TTVTaw_1F85KjMtVuQwen50FFpoxN3UajeIhJKq3kGbTUYskxDNoUqSXEKjXzO5Q%3D%3D; memclid=56a08146-0c8d-4804-a0fd-b4f260fa97c7; NetflixId=v%3D2%26ct%3DBQAOAAEBEPWqAdwhDzT9fGigJ17MprOA8HnnmJwbPXxVXQU6l7-AoAf0CdmoJ4OKFuXNmN_Gk1VSxDTfOlR5l6vTdpv2E5uDgM_-TOYTDJUAn6dasLtySfwFb36rx9zlcFZXd2V4ev_Phv7xeXEiIHg2-07r9D_-lk1mOvi0vrkA4Ks0fDdZPUKNzxSv6I9wG1JTy6VgQJWgmHohn0pZllDVFPTgtK7w80Z6zk8HpsS2NhHVN5fFtpDltPxcyhgvpSKoubPjI34tHyi5mdZ4wFWS5Fr-M9mSften3mCFSEbbD60owhL0UqJSTgqj0fEMcvWTHOHyrmMh2mNsnULZLssEAX8nLODtEg..%26bt%3Ddev%26mac%3DAQEAEAABABRtx6338QBfcQqapPEDcdramTVeKrC8MEo.; SecureNetflixId=v%3D2%27mac%3DAQEAEQABABSZexlJGSn3i-7avPfXiwl-aToM9mLR0Js.%26dt%3D1563037431438; flwssn=e448dd2d-4808-4bde-ac20-d2c32f5fc16d; clSharedContext=698074de-40c7-48a6-bf24-2738230a2f51; hasSeenCookieDisclosure=true; didUserInteractWithPage=true; dsca=anonymous; cL=1563038693194%7C156303869337943053%7C156303869386624258%7C%7C4%7Cnull",
    "Host": "www.netflix.com",
    "Origin": "https://www.netflix.com",
    "Referer": "https://www.netflix.com/login",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
}

data = {
    "userLoginId": LOGIN,
    "password": PASSWORD,
    "rememberMe": "true",
    "flow": "websiteSignUp",
    "mode": "login",
    "action": "loginAction",
    "withFields": "rememberMe,nextPage,userLoginId,password,countryCode,countryIsoCode",
    "authURL": authURL,
    "nextPage": "",
    "showPassword": "",
    #"countryCode": "+49",
    #"countryIsoCode": "DE"
}

r1 = s.post(URL_LOG, data=data, headers=headers)
r2 = s.get(URL_MAIN)

print(r1.url)
print(r2.url)

From the rearrangement of terms, so to speak ...
Instead of the expected link, my get request returns (already disturbing my not very clear eyes) a link to the authorization page.
23:28 - Tomorrow I will continue to beat against the wall, for today, perhaps, that's enough.
07/14/2019
Good morning! Let's continue ... Today I'll try to dig in the direction of cookies.
During this day, I did not do anything sensible, today I enjoyed the series.
07/15/2019
Today again without any results, BUT I waited for the answers that I undertake to think over and study.
There is not enough space for subsequent comments (maximum 10 thousand characters). We have to remove the codes of Implementations 1 and 2. It's okay, there was nothing interesting in them anyway.
DAMN! MADE!!! WORKS! 07/15/2019 23:29 Now run to finalize, I will try to embed a proxy.
Question:
So how can I implement authorization, while, preferably, returning True / False?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
R4ndolphC4rter, 2019-07-15
@R4ndolphC4rter

THANKS TO ALL! QUESTION CLOSED .
URL_LOG is a constant.
data - a dictionary whose dynamic values ​​are login & password, authURL.
headers - a dictionary whose dynamic value (text variable) is cookies.
All other elements of the dictionaries are static and pulled straight from the site through the developer console.

A
Alexey, 2019-07-12
@Messiah_v2

As far as I know, with any authorization, a certain token is issued, which needs to be written into the session cookie.
Follow the authorization trace in the browser. Look at cookies before and after authorization. I think the answer lies there

N
Nastya Sukharik, 2019-07-12
@nastya_cyxarik

have you tried selenium?

F
fire_engel, 2020-05-06
@fire_engel

R4ndolphC4rter , please write back how you got the necessary cookies and what code you ended up with.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question