我正在尝试使用python中的请求模块登录我的Morningstar.com高级帐户,如下所示。 post命令以状态200运行,但实际上并不登录。
(当我下载资产负债表时,我只收到5年(非高级)版本而不是请求的10年(高级)版本。这表明我的登录脚本失败,因为5年数据无需登录即可使用。资产负债表网址在浏览器中手动登录时正常工作。)
有人知道如何正确设置登录脚本?
看起来非常直接,但我已经尝试了一整天使用不同形式的有效载荷/标题等,并且找不到正确的方法...另外,我很困惑因为我找不到表单数据信息时检查登录页面。
import csv
import requests
urlLogin = 'http://members.morningstar.com/memberservice/login.aspx'
urlBalanceSheet = 'http://financials.morningstar.com/ajax/ReportProcess4CSV.html?&t=XNYS:F®ion=usa&culture=en-US&cur=&reportType=bs&period=12&dataType=A&order=desc&columnYear=10&rounding=1&view=raw&r=149906&denominatorView=raw&number=1'
payload = {
"uEmail": "<userEmail>",
"uPassword": "<userPW>",
"remember_me": "on",
"login": "Sign In"
}
with requests.Session() as s:
p = s.post(urlLogin, data = payload)
print(p.status_code)
download = s.get(urlBalanceSheet)
答案 0 :(得分:1)
您可以采取一些措施来自动从morningstar下载
pip install selenium http://selenium-python.readthedocs.io/installation.html
安装firefox,找出您的个人资料所在的位置是资源http://toolsqa.com/selenium-webdriver/custom-firefox-profile/
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
import requests
from xml.etree import cElementTree as ET
import csv
from selenium.webdriver.common.action_chains import ActionChains
def timeme(method):
def wrapper(*args, **kw):
startTime = int(round(time.time() * 1000))
result = method(*args, **kw)
endTime = int(round(time.time() * 1000))
print(endTime - startTime, 'ms')
return result
return wrapper
class Driver():
def __init__(self,profile, diver_path, url):
self.profile = profile
self.driver_path = diver_path
self.url = url
def start_driver(self):
user_profile = webdriver.FirefoxProfile(self.profile)
user_profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'text/csv')
driver = webdriver.Firefox(executable_path=self.driver_path, firefox_profile=user_profile)
driver.get(self.url)
return driver
def shutdown(self,driver):
driver.quit()
@timeme
def login(driver, email = '', password = ''):
wait_time = 1
try:
email_input = WebDriverWait(driver,wait_time).until(
EC.presence_of_all_elements_located((By.XPATH,'//*[@id="uim-uEmail-input"]')))
email_input = driver.find_element_by_xpath('//*[@id="uim-uEmail-input"]').send_keys(email)
time.sleep(5) # wait time to see if you have input remove later
pwd_input = driver.find_element_by_xpath('//*[@id="uim-uPassword-input"]').send_keys(password)
time.sleep(5)
sign_in = driver.find_element_by_xpath('//*[@id="uim-login-submit"]').click()
title = driver.title
driver.execute_script("window.open('http://financials.morningstar.com/ajax/ReportProcess4CSV.html?&t=XNYS:F®ion=usa&culture=en-US&cur=&reportType=bs&period=12&dataType=A&order=desc&columnYear=10&rounding=1&view=raw&r=149906&denominatorView=raw&number=1','new_window');")
time.sleep(1)
return 0
except Exception as e:
return None
@timeme
def main():
# i am using on my mac, if you are using windows change paths accordingly
Mozilla = Driver(profile = '/Users/yourname/Library/Application Support/Firefox/Profiles/xxxxxxxxxxxx.default',
diver_path='/usr/local/bin/geckodriver', # path to firefox driver
url='https://www.morningstar.com/members/login.html?vurl=')
driver = Mozilla.start_driver()
download = login(driver, password='', email='')
if download ==0:
time.sleep(10) # let browser to download csv
Mozilla.shutdown(driver) # shutdown
main()