Wednesday, 1 March 2023

Unable to log in to a website using the requests module

I'm trying to log in to a website using requests module. It seems I have incorporated the manual steps into the script based on what I see in dev tools while logging in to that site manually. However, when I run the script and check the content it received as a response, I see this line: There was an unexpected error.

I've created a free account there for the purpose of testing only. The login details are hardcoded within the parameters.

import requests
from bs4 import BeautifulSoup

link = 'https://www.apartments.com/customers/login'
login_url = 'https://auth.apartments.com/login?{}'
params = {
    'dsrv.xsrf': '',
    'sessionId': '',
    'username': 'shahin.iqbal80@gmail.com',
    'password': 'SShift1234567$'
}

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}

headers_post = {
    'origin': 'https://auth.apartments.com',
    'referer': '',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9,bn;q=0.8',
    'x-requested-with': 'XMLHttpRequest'
}
with requests.Session() as s:
    s.headers.update(headers)
    resp = s.get(link)
    soup = BeautifulSoup(resp.text,"lxml")
    res = s.get(soup.select_one("#auth-signin-iframe")['src'])
    soup = BeautifulSoup(res.text,"lxml")
    post_url = login_url.format(soup.select_one("[id='signinform']")['action'].split("/login?")[1])
    headers_post['referer'] = post_url
    s.headers.update(headers_post)
    params['dsrv.xsrf'] = soup.select_one("input[name='idsrv.xsrf']")['value']
    params['sessionId'] = soup.select_one("input[id='sessionId']")['value']
    resp = s.post(post_url,data=params)
    print(resp.status_code)
    print(resp.content)
    print(resp.url)

How can I make the login successful using the requests module?



from Unable to log in to a website using the requests module

No comments:

Post a Comment