1 回答

TA贡献1847条经验 获得超7个赞
有效负载数据来自登录页面本身,因此可以https://urs.earthdata.nasa.gov/查看浏览器上的网络选项卡。
我刚刚输入了一些随机的用户名和密码,然后查看我的网络选项卡,我看到了一个 POST https://urs.earthdata.nasa.gov/login。查看有效载荷,这是它的格式:
utf8: ✓
authenticity_token: ...token base64...
username: 123
password: 123
client_id:
redirect_uri:
commit: Log in
所以我们只需authenticity_token要从源中提取。查看登录页面的源代码,我们看到了这一点:
<form id="login" action="/login" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="...token base64..." />
所以我们只是使用一些正则表达式来提取这个位,因为它对于像这样的一次性东西更快(你可以使用任何其他方法或你想要的正则表达式形式):
token = re.search(r'authenticity_token".*?"(.*?)"', webpage.text).group(1)
最后创建数据并将其传递给 POST 方法:
data = {
"utf8": "✓",
"authenticity_token": token,
"username": username,
"password": password,
"client_id": "",
"redirect_uri": "",
"commit": "Log in",
}
login = requests.post("https://urs.earthdata.nasa.gov/login", headers={'User-Agent':'Mozilla/5.0'}, data=data)
添加回答
举报