I've been trying to login to a website using python so I can access some photos that are behind a login screen. I've seen a bunch of examples here, but none of them seem to work. Here is my code:
#!/usr/local/bin/python3
import requests
from bs4 import BeautifulSoup
if __name__ == "__main__":
s = requests.Session()
url = 'http://www.usafawebguy.com/Account/Login'
g = s.get(url)
token = BeautifulSoup(g.text, 'html.parser').find('input',{'name':'__RequestVerificationToken'})['value']
print (token)
payload = { 'UserName': 'username',
'Password': 'password',
'RememberMe': 'true',
'ReturnURL': '/Photos/2022?page=1',
'__RequestVerificationToken': token }
p = s.post(url, data=payload)
soup = BeautifulSoup(p.text, 'html.parser')
print (soup.title)
#print (p.text)
r = s.get('https://www.usafawebguy.com/Photos/2022?page=1')
soup = BeautifulSoup(r.text, 'html.parser')
print (soup.title)
#print (r.text)
It always brings me back to the login screen. Thanks in advance!
A one character change: The url variable needs to be set to: "https://....", not "http://....". Live and learn.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With