问题
I'm trying to log in to the Virtual Trader website using the python requests module - I have limited experience in this area so would appreciate some help.
Previously I have used the following code to log in to a similar website:
import requests
USERNAME = 'VALID EMAIL'
PASSWORD = 'VALID PASSWORD'
page = {
'index':'http://www.bullbearings.co.uk/',
'login':'http://www.bullbearings.co.uk/login.php',
'trade':'http://www.bullbearings.co.uk/stock.trade.php?epic=',
'portfolio':'http://www.bullbearings.co.uk/stock.portfolio.php'
}
def login(access):
with requests.Session() as c:
c.get(page['login'])
login_data = dict(email = USERNAME, password = PASSWORD, submit = 'Login', cookie = 'yes')
c.post(page['login'], data = login_data, headers = {'Referer': page['index']})
source = c.get(page[access])
print source.content
.. I've attempted to deploy similar code to automate login for the Virtual Trader site, however have so far been unsuccessful. My amended code is shown below - I have withheld personal information from each of the examples for security reasons - Accounts can be opened in a matter of minutes for testing purposes, alternatively solutions can be submitted back to this post and I'll be on hand to test if it works and report back.
Attempt to modify code for use with Virtual Trader
import requests
with requests.Session() as c:
url = 'http://www.virtualtrader.co.uk/member/login.aspx'
USERNAME = 'VALID USERNAME'
PASSWORD = 'VALID PASSWORD'
email = 'ctl00$MiddleContent$LoginForm$txtEmail'
password = 'ctl00$MiddleContent$LoginForm$txtPassword'
c.get(url)
login_data = dict(email = USERNAME , password = PASSWORD)
c.post(url, data = login_data, headers = {'Referer': 'http://www.virtualtrader.co.uk'})
page = c.get('http://www.virtualtrader.co.uk/member/personal.aspx')
print page.content
This one has beaten me! - I'd really appreciate any help trying to solve this. Personally I prefer using the python requests library, however if you are able to solve this using an alternative I would be glad to change.
I appreciate this is a very specific question - sorry!
Thanks in advance
回答1:
This should get you started:
from selenium import webdriver
USERNAME = 'VALID USERNAME'
PASSWORD = 'VALID PASSWORD'
email = 'ctl00$MiddleContent$LoginForm$txtEmail'
password = 'ctl00$MiddleContent$LoginForm$txtPassword'
submit_id = 'ctl00_MiddleContent_LoginForm_cmdLogin'
driver = webdriver.Chrome() # could also use firefox, or a bunch of others listed on the homepage of selenium
driver.get('http://www.virtualtrader.co.uk/member/login.aspx')
login_field = driver.find_element_by_id(email)
password_field = driver.find_element_by_id(password)
submit_field = driver.find_element_by_id(submit_id)
login_field.send_keys(USERNAME)
password_field.send_keys(PASSWORD)
submit_field.click()
If you would like further help, leave a comment here.
来源:https://stackoverflow.com/questions/25046314/log-in-to-www-virtualtrader-co-uk-using-python-requests-or-similar-module