首页 > 解决方案 > 使用 requests.session 登录后无法访问其他站点

问题描述

这是我在这里的第一个问题,我对编程也很陌生,所以请在回答时考虑一下:)

我正在尝试通过 python 创建一个带有“BeautifulSoup”和“请求”的网页抓取工具,用于梦幻德甲游戏,我想在其中收集所有玩家数据。我想我已经设法登录到该站点,但我没有正确登录。我创建了一个会话并使用我的登录信息发送了一个有效负载。之后,我想在需要登录用户的不同页面中做任何我喜欢的事情。但我总是被重定向到主页。

正常的登录页面是https://fantasy.bundesliga.de/?register_or_login=login。使用开发者工具,我找到了正确的 url 来模拟登录为“https://accounts.eu1.gigya.com/accounts.login”。表单数据有很多参数,我不确定如何正确操作它们。在第一次尝试中,我只是从手动登录中复制了参数。但是当我尝试访问另一个网站时,它会将我重定向到主页“https://fantasy.bundesliga.de/”。

到目前为止,这是我的代码:

import requests
from bs4 import BeautifulSoup

payload = {"loginID": "replace with E-Mail",
"password": "replace with password",
"sessionExpiration": "0",
"targetEnv": "jssdk",
"include": "profile,data,emails,subscriptions,preferences,",
"includeUserInfo": "true",
"loginMode": "standard",
"lang": "de",
"APIKey": "3_2B6SeL1zQuVojVM9IlfzLovH7xJLlpUIeSXNPMS0uihudZQtuKy99SfKcrJRtWKo",
"source": "showScreenSet",
"sdk": "js_latest",
"authMode": "cookie",
"pageURL": "https://fantasy.bundesliga.de/",
"format": "json",
}

with requests.Session() as s:
    p = s.post("https://accounts.eu1.gigya.com/accounts.login", data = payload)
    print(p.url)
    f = open("login.txt", "w", encoding="utf-8")
    f.write(p.text)
    f.close()
    r = s.get("https://fantasy.bundesliga.de/match_days/14/lineups/252439")
    print(r.url)

login.txt 包含我所有的个人资料信息,所以我已经成功登录。但是最后一行,我想打印 r.url,输出是“https://fantasy.bundesliga.de/”,所以我无法访问我想要的信息。

老实说,我一无所知,因为我在网上找不到这个问题。每个人在登录后似乎都能成功访问他们的页面。

谢谢大家解答!

标签: pythonauthenticationweb-scrapingpython-requests

解决方案


推荐阅读