python - Login function in separate file using RoboBrowser -
i writing program/script retrieve key information hoovers.com. have 2 files. 1 hoovers5.py
(which contains, among other things, classes) , other main.py
.
hoovers5.py
:
def testlogin(): import hlogin # file contains hoovers login credentials robobrowser import robobrowser browser = robobrowser() login_url = "https://subscriber.hoovers.com/h/login/login.html" browser.open(login_url) form = browser.get_form(id='loginform') # gets login form id form['j_username'].value = hlogin._hname # fills in hoovers login name form['j_password'].value = hlogin._hpass # fills in hoovers password browser.submit_form(form) # submits login
main.py
:
import hoovers hv5 robobrowser import robobrowser hv5.testlogin() browser = robobrowser() browser.open('http://subscriber.hoovers.com/h/company360/overview.htmlcompanyid=26082233') print browser.parsed
i have tested code numerous times , login works if run login without placing function in main file. works in sense can execute following , expected parsed value:
browser.open('http://subscriber.hoovers.com/h/company360/overview.htmlcompanyid=621076715') print browser.parsed
however, create function login, login doesn't seem to...stick? losing session in hoovers5.py
file because i'm instantiating new browser in main.py
? how can pass "session" main script? i've tried creating object called browser2 = robobrowser()
, opening url through that, doesn't work.
do require cookies (and if, how go this?) or need return in function?
your function creates new robobrowser()
, separate state. if opened new browser (not new window or tab) , tried use login session there.
share browser
object between function , rest of code:
def testlogin(browser): import hlogin # file contains hoovers login credentials login_url = "https://subscriber.hoovers.com/h/login/login.html" browser.open(login_url) form = browser.get_form(id='loginform') # gets login form id form['j_username'].value = hlogin._hname # fills in hoovers login name form['j_password'].value = hlogin._hpass # fills in hoovers password browser.submit_form(form) # submits login
and use as:
import hoovers hv5 robobrowser import robobrowser browser = robobrowser() hv5.testlogin(browser) browser.open('http://subscriber.hoovers.com/h/company360/overview.htmlcompanyid=26082233') print browser.parsed
now have one browser
object, login session stored in object , can continue use session other urls.
Comments
Post a Comment