Having Trouble With Python Login Bot

Question:

I am currently trying to create a python bot using mechanize that scrapes my account for a school project however I am having trouble logging in to this website: https://marketwatch.com/login

import mechanize
loginurl = https://marketwatch.com/login
user = raw_input("enter user")
passcode = raw_input("enter passcode")
browser = mechanize.Browser()
browser.set_handle_robots(False)
browser.open(loginurl)
browser.select_form(nr=0)
browser.form["username"] = user
browser.form["password"] = passcode
browser.submit()
print(browser.title())

After I run the code it doesn’t login, but instead it remains on the login page. Any possible solutions? Any help would be greatly appreciated!

Asked By: alf

||

Answers:

As jm_____ said selenium is what you’re looking for.

Selenium can be found here

Here is a login script utilizing selenium. Ill leave the scraping of whichever page comes after the login to you.

import selenium
from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://marketwatch.com/login')
username = browser.find_element_by_name("username")
password = browser.find_element_by_name("password")
submit = browser.find_element_by_id("submitButton")
username.send_keys("username")
password.send_keys("password")
submit.click()
Answered By: Colabambino