使用Python请求登录Morningstar.com [英] Morningstar.com login using Python Requests
问题描述
我正尝试使用python中的请求模块,登录我的Morningstar.com高级帐户,如下所示. post命令以状态200运行,但实际上并没有使我登录.
I am trying to log in to my Morningstar.com premium account using the requests module in python as below. The post command runs through with status 200 but does not actually log me in.
(当我下载资产负债表时,我只收到5年(非高级)版本,而不是要求的10年(高级)版本.这表明我的登录脚本因5年数据而失败无需登录即可使用.在浏览器中手动登录时,资产负债表URL正确运行.)
有人知道如何正确设置登录脚本吗?
这似乎很简单,但是我整天都在尝试使用不同形式的有效负载/标头等,但是找不到正确的方法...而且,我很困惑,因为在以下情况下找不到表单数据信息检查登录页面.
It seems very straight forward but I have tried the whole day using different forms of the payload/ headers etc. and can't find the right way... Also, I am confused since I cannot find the Form Data information when inspecting the login page.
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)
推荐答案
您可以执行几项操作来自动从Morningstar自动下载
There are few things you can do to automate downloading from morningstar
pip安装硒 http://selenium-python.readthedocs.io/installation.html
安装firefox,找出您的个人资料在哪里是资源 http://toolsqa.com/selenium-webdriver/custom-firefox-profile/
install firefox, find out where your profile is here is an resource 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()
这篇关于使用Python请求登录Morningstar.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!