python两步登录
问题描述:
我想从仪表板环境下载python脚本中的文件并操作文件中的数据。仪表板环境需要我登录两次。我首先需要登录到公司帐户,然后进入个人帐户。我可以登录公司帐户,但是当我提供正确的凭据时,登录到我的个人帐户失败。python两步登录
这是我正在尝试使用的脚本。恒星之间的东西是改变了隐私的原因:
import csv
import requests
URL_Login = '*baseurl of the dashboard*'
CSV_URL = '*baseurl of the dashboard*/auto/reports/responses/?sheet=1528&item=4231&format=csv'
with requests.Session() as s:
download = s.get(URL_Login, auth=("*corporate account name*", "*corporate password*"))
download = s.get(CSV_URL, auth=("*personal account name*", "*personal password*"))
decoded_content = download.content.decode('utf-8')
cr = csv.reader(decoded_content.splitlines(), delimiter=',')
my_list = list(cr)
for row in my_list:
print(row)
我收到以下错误信息: 401 - 未经授权:访问被拒绝由于凭据无效。 您无权使用您提供的凭据查看此目录或页面。
什么都可以触发401,因为我确信我提供了正确的凭据?
答
要让页面处理第一个请求,请在第二个下载语句之前尝试一个定时器:download s.get ...,例如time.sleep(3)等待3秒钟(将其延长到最大值如果3秒不起作用,则通过反复试验约7秒)。首先导入时间。
它仍然无法正常工作,这意味着你的request.session()需要被再次调用,所以尝试:
import csv
import requests
URL_Login = '*baseurl of the dashboard*'
CSV_URL = '*baseurl of the dashboard*/auto/reports/responses/?sheet=1528&item=4231&format=csv'
with requests.Session() as s:
download = s.get(URL_Login, auth=("*corporate account name*", "*corporate password*"))
time.sleep(3)
with requests.Session() as t:
download = t.get(CSV_URL, auth=("*personal account name*", "*personal password*"))
decoded_content = download.content.decode('utf-8')
cr = csv.reader(decoded_content.splitlines(), delimiter=',')
my_list = list(cr)
for row in my_list:
print(row)
...第三干预,如果第二也不管用,是回到您的原代码,并且将其添加到“与requests.Session()为s:”行,以使Cookie的持续,所推荐的第一个答案here:
with requests.Session(config={'verbose': sys.stderr}) as s:
如果出现任何问题,请留下评论。
@diamini,不幸的是,仍然无法正常工作。也许它取决于浏览器提供/提供的内容,而请求库没有? – user2003846
@ user2003846,感谢您的反馈。需要进一步思考。 – Dlamini
@ user2003846。我在回答中添加了第三个建议。希望它给你一个解决方案的更多想法。 – Dlamini