Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login to Website using Python and __RequestVerificationToken

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!

like image 724
Phil I. Avatar asked Oct 25 '25 00:10

Phil I.


1 Answers

A one character change: The url variable needs to be set to: "https://....", not "http://....". Live and learn.

like image 62
Phil I. Avatar answered Oct 27 '25 15:10

Phil I.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!