0

Im trying to login to a website and keep a session so I can post multiple data on to it later on. Im using a with statement to do that. Im having trouble posting my login credentials, I'm trying to do something similar as the answer here https://stackoverflow.com/a/15794896/12203664 specifically when posting the login although it was from 8 yrs ago.

I get an AttributeError: 'Response' object has no attribute 'post'. I think it is because session now does not have a post method? I'm not sure how to go about that. Below is a sample of what I have so far.

import requests
from bs4 import BeautifulSoup

url="https://website.com/login"
def login_payload(html):
    login_payload = {'user': 'USER',
                'pass': 'PW',
                'login': loginValue(html)     }
    return login_payload

def loginValue(html):
    soup = BeautifulSoup(html, 'html.parser')
    hidden_tags = soup.find_all(type='hidden')
    for tag in hidden_tags:
        return tag.get('value')

with requests.Session() as s:
    s = s.get(url) #to get loginvalue
    s = s.post(url, data = login_payload(s.text))
    print(s.text) #check if login is successful
MT22
  • 33
  • 6
  • you use the same name `s` for `Session()` and for result from `s.get()` and this makes problem.You should rename one of them - ie. `response = s.get(...)` , `response = s.post( response.text...)` `print(response.text)` – furas Mar 26 '22 at 03:46
  • if you check code in your link then you see `r = s.post()` - it uses `r` for result from `post()` - and if you would use the same in your code (`r = s.get()` and `r = s.post()` ) then you wouldn't have problem – furas Mar 26 '22 at 03:50

0 Answers0