I am trying to login to my gym and scrape the data so I can setup automatic login/reservations for the future. I am very new to python, I have tried a few things and I think this is the closest. I found a great example online but having issues. I think the login is failing making the code not access the correct page to scrape. I used XPath helper and it confirmed my xpath was correct but my result is blank [] Please see below.. any suggestions would be appreciated.
import requests
from lxml import html
USERNAME = "*****"
PASSWORD = "*****"
LOGIN_URL = "https://www.lafitness.com/Pages/login.aspx"
URL = "https://www.lafitness.com/Pages/RacquetballReservation.aspx"
def main():
session_requests = requests.session()
# Get login csrf token
result = session_requests.get(LOGIN_URL)
tree = html.fromstring(result.text)
authenticity_token = list(set(tree.xpath("//input[@name='__CSRFTOKEN']/@value")))[0]
# Create payload
payload = {
"ctl00$MainContent$Login1$txtUser": USERNAME,
"ctl00$MainContent$Login1$txtPassword": PASSWORD,
"__CSRFTOKEN": authenticity_token
}
# Perform login
result = session_requests.post(LOGIN_URL, data = payload, headers = dict(referer = LOGIN_URL))
# Scrape url
result = session_requests.get(URL, headers = dict(referer = URL))
tree = html.fromstring(result.content)
bucket_names = tree.xpath('//*[@id="ddlDates"]')
ok = result.ok
print(ok, bucket_names)
if __name__ == '__main__':
main()
I have looked up several items trying to do this but have been unsuccessful. I added the result.ok to see if it's connecting correctly, which it says it does, but I have read just because it says positive doesn't mean the website logged in correctly.
Thanks for any help/suggestions!