从需要登录的URL中获取JSON数据

2024-09-27 23:23:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试这个代码。在

#This URL will be the URL that your login form points to with the "action" tag.
POST-LOGIN-URL = 'https://my.freecycle.org/login'

#This URL is the page you actually want to pull down with requests.
REQUEST-URL = 'https://my.freecycle.org/home/posts'


payload = {
    'username': 'my_UN',
    'pass': 'my_pswd'
}

with requests.Session() as session:
    post = session.post(POST-LOGIN-URL, data=payload)
    r = session.get(REQUEST-URL)
    print(r.text)   #or whatever else you want to do with the request data!

我从下面的链接中找到了那个代码。在

https://pybit.es/requests-session.html

剧本非常简单明了。我输入了我的真实凭证,得到了401响应(未经授权)。这说明我没有权限使用我输入的凭据访问URL,但是我知道当我登录到URL的主登录页面时,这些凭据工作正常。我是不是遗漏了一些简单的东西,或者这不是为我上面描述的设置而设计的?在


Tags: theto代码httpsorgurlsessionmy
2条回答

这是我最后的解决办法。在

import requests 
import json
import base64

REQUEST_URL = 'your_JSON_URL'
login = 'FN.LN@your_email.com'
password = 'your_PW'
response = requests.get(REQUEST_URL, auth=(login, password))

json_data = response.text.encode('utf-8', 'ignore')

分享这个来帮助那些在我之后来到这里的人。在

“payload”变量似乎没有所有正确的组件。虽然在外部对用户来说,表单只需要用户名和密码,但除了表单提交后发出的POST请求之外,这些不是唯一的值。让我们仔细研究一下https://my.freecycle.org/login的登录形式。在

要使程序成功,您需要在表单中包含所有输入。您有用户名和密码字段,但缺少名为“referer”且值为“”的隐藏字段。它应该在添加此项时起作用。通常,隐藏字段用于提交用户不输入的信息。因此,通过将有效载荷改为:

payload = {
'username': 'my_UN',
'pass': 'my_pswd',
'referer': ''
}

您应该能够成功登录。在

相关问题 更多 >

    热门问题