3

I am trying to automatically scrape my balance info from my Vanguard investment account. I am stuck trying to login to the account.

I've tried requests and mechanicalsoup and so far I have not been able to figure this out.

import mechanicalsoup



USERNAME = "myuser"
PASSWORD = "mypass"

OWNER="myemail"

start_page = "https://personal.vanguard.com/us/hnwnesc/nesc/LoginPage"
balance_page = "my balancepage"

r = mechanicalsoup.StatefulBrowser()
r.addheaders = [ 
("User-agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv 1.0) %s" %
               (OWNER)),
]


r.open(start_page)
r.select_form('LoginForm')
r['USER'] = USERNAME
r = br.submit()

r.select_form('LoginForm')
r['PASSWORD'] = PASSWORD
r = br.submit()

r = br.open(balance_page)

I am expecting to login and be directed to balance page, in order to automatically scrape balances to put into a dataframe.

EDIT here is a semi-working update using Selenium

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://investor.vanguard.com/my-account/log-on")
user_button = driver.find_element_by_id('USER')
user_button.send_keys('UsErId')
password_button = driver.find_element_by_id('PASSWORD')
password_button.send_keys('P@$$W0rd')

button = driver.find_element_by_id('login')
button.click()
John
  • 43
  • 5
  • Please explain what result you get with this code, and why it's not what you expect. We can't answer if there's no question ;-). Does the site use JavaScript? – Matthieu Moy Sep 09 '19 at 06:07
  • @Matthieu-Moy Apparently Selenium is a better tool for this since it does in fact use JavaScript? In my original script it just returns the HTML (I think?) of the login page...it doesn't actually log me in and can't find the 'USER' or 'PASSWORD' forms – John Sep 09 '19 at 13:59
  • If you need JavaScript, then yes, Selenium is not just better, it's the solution that works. MechanicalSoup is just a lightweight library for web scrapping, but it's much more limited. – Matthieu Moy Sep 10 '19 at 18:16

0 Answers0