Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何在不使用selenium等无头浏览器的情况下登录morningstar.com?_Python_Python 3.x_Post_Get_Python Requests - Fatal编程技术网

Python 我如何在不使用selenium等无头浏览器的情况下登录morningstar.com?

Python 我如何在不使用selenium等无头浏览器的情况下登录morningstar.com?,python,python-3.x,post,get,python-requests,Python,Python 3.x,Post,Get,Python Requests,我宣读了对问题的答复: 如何使用Python的请求模块“登录”网站 答案是: 首先检查登录表单的来源,以获得三条信息—表单发布到的url,以及用户名和密码字段的名称属性 如何查看此morningstar.com页面的用户名和密码的名称属性? 我有以下代码: import requests url = 'http://www.morningstar.com/members/login.html' url = 'http://beta.morningstar.com' with open('m

我宣读了对问题的答复: 如何使用Python的请求模块“登录”网站

答案是: 首先检查登录表单的来源,以获得三条信息—表单发布到的url,以及用户名和密码字段的名称属性

如何查看此morningstar.com页面的用户名和密码的名称属性?

我有以下代码:

import requests

url = 'http://www.morningstar.com/members/login.html'
url = 'http://beta.morningstar.com'

with open('morningstar.txt') as f:
    username, password = f.read().splitlines()

with requests.Session() as s:
    payload = login_data = {
        'username': username,
        'password': password,
        }
    p = s.post(url, data=login_data)
    print(p.text)
但是,除其他外,它还打印:

This distribution is not configured to allow the HTTP request method that was used for this request. The distribution supports only cachable requests.
帖子的url和数据应该是什么


还有另一种是使用selenium,但是否可以避免这种情况?

如代码所示,用户名输入字段是:

<input id="uim-uEmail-input" name="uEmail" placeholder="E-mail Address" data-msat="formField-inputemailuEmail-login" type="email">
<input id="uim-uPassword-input" name="uPassword" placeholder="Password" data-msat="formField-inputpassworduPassword-login" type="password">
密码输入字段为:

<input id="uim-uEmail-input" name="uEmail" placeholder="E-mail Address" data-msat="formField-inputemailuEmail-login" type="email">
<input id="uim-uPassword-input" name="uPassword" placeholder="Password" data-msat="formField-inputpassworduPassword-login" type="password">
在name=之后的每一行中都列出了这两个名称:

用户名:uEmail
密码:uPassword

这有点难,我不得不使用拦截代理,但它是:

import requests

s = requests.session()
auth_url = 'https://sso.morningstar.com/sso/json/msusers/authenticate'
login_url = 'https://www.morningstar.com/api/v2/user/login'
username = 'username'
password = 'password'

headers = {
    'Access-Control-Request-Method': 'POST',
    'Access-Control-Request-Headers': 'content-type,x-openam-password,x-openam-username',
    'Origin': 'https://www.morningstar.com'
}
s.options(auth_url, headers=headers)

headers = {
    'Referer': 'https://www.morningstar.com/members/login.html',
    'Content-Type': 'application/json',
    'X-OpenAM-Username': username,
    'X-OpenAM-Password': password,
    'Origin': 'https://www.morningstar.com',
}
s.post(auth_url, headers=headers)

data = {"productCode":"DOT_COM","rememberMe":False}
r = s.post(login_url, json=data)

print(s.cookies)
print(r.json())
现在您应该已经有了一个经过身份验证的会话。你应该在s.cookies中看到一堆cookies,在r.json中看到一些关于你帐户的基本信息

该网站更改了登录机制,可能还更改了整个CMS,因此上述代码不再有效。新的登录过程包括一个POST和一个对/umapi/v1/sessions的补丁请求,然后是对/umapi/v1/users的GET请求


URL和其他所需的值(如POST数据)可以从web浏览器的开发人员控制台Ctrl+Shift+I中的“网络”选项卡下获得。

Ahhh,是的。我在Google Chrome中使用了Inspect,它出现了。在查看页面源代码时,我看不到它。它回答了我的问题,但我仍然无法登录。你介意我等一下再接受你的回答吗?谢谢@汤米,卡斯滕森,没问题!很乐意帮忙!如前所述,你的回答对我的原始问题是正确的,但另一个回答回答了我的真实问题,我将选择他的答案作为正确答案。但是,在我编辑之前,谢谢你回答了问题的原始版本。谢谢可能是重复的是的,我不知道是否有可能的要求。似乎已向提交POST请求https://sso.morningstar.com/sso/json/msusers/authenticate 使用X-OpenAM标题中的登录数据。但我不认为这是重复的,因为您链接的帖子使用了seleniumIt!之后,您可以在登录时执行r=s。获取“其他晨星url”。你没有回答我的问题,但你回答了我真正的问题。当我读到这些伟大的答案时,我觉得自己太傻了。非常感谢你!我自己怎么能想出这个答案呢?通常你必须在浏览器Inspect>network中监控网络流量,但在这种情况下,你需要一个拦截代理或wireshark或类似工具,因为有很多请求/重定向。在我的代码中,你会注意到三个请求,一个选项,两个帖子。这对我来说也是一个挑战!这家伙似乎也有同样的问题:他们似乎更改了CMS,所以我不得不更新代码。请注意,新内容是动态的,因此可能不容易通过请求将其删除。