jeudi 19 juillet 2018

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!




Aucun commentaire:

Enregistrer un commentaire