gpt4 book ai didi

python 请求登录网站

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:42 25 4
gpt4 key购买 nike

我正在学习如何使用 pythonrequests 登录网站,并浏览了 stackoverflow 上的许多不同帖子和 youtube 上的视频,了解需要什么以及如何登录去做吧。

当我在浏览器上单击“提交”时,我发现以下信息正在通过表单发送。 我转到网络下的开发人员工具并查看了标题

form response when submitting on website

我能告诉你什么

我可以告诉的是,在登录页面本身,他们只要求用户名密码,其中以下代码是html > 提取。

<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="T8NxfsxCHqUPzdvmM++VIpipimDyjsLHkg4Oz3Yuouk="></div>
<ul class="sic_loginFailed">
<li>
<label for="sic_login_header_username">Username</label>
<input id="sic_login_header_username" name="name" type="text" class="sic_formText">
</li>
<li>
<label for="sic_login_header_password">Password</label>
<input id="sic_login_header_password" name="password" type="password" class="sic_formText">
</li>
<li class="sic_remember">
<input id="sic_login_header_remember" name="remember" type="checkbox">
<label for="sic_login_header_remember">Remember my login.</label>
</li>
<li>
<input type="hidden" name="redirect"
value="https://www.shareinvestor.com/sg">



<input id="sic_login_submit" type="submit" value="Sign In" class="sic_greenInputButton">
</li>
</ul>

这意味着authenticity tokenpassword_m是由网站自动生成的?注意:我感觉 password_m 是在我创建帐户时自动分配给我的。但 token 是在每次登录时自动生成的。

我的问题

我根据我所知道的和我所研究的内容编写了以下代码,但我仍然无法登录该网站。

url = "https://www.shareinvestor.com/user/login.html"  # This is the main URL login page

login_data = {'name': 'test_user',
'password': 'test_password',
'password_m': '5d93ceb70e2bf5daa84ec3d0cd2c731a',
'remember': True,
'redirect': 'https://www.shareinvestor.com/sg'}

with requests.Session() as s:
a = s.get(url).text
b = bs4.BeautifulSoup(a, 'lxml')
c = b.findAll('input', type='hidden') # This is to draw out the token. I tried searching for it in the cookies previously, but failed badly....
for i in c:
login_data[i['name']] = i['value']

# I use the this url for the response because as per the `Headers` in the picture above, it says that this is the request URL that the form is submitting to.
response = requests.post('https://www.shareinvestor.com/user/do_login.html?use_https=1', data=login_data)
response = requests.get('https://www.shareinvestor.com/user/edit_profile.html', cookies=response.cookies)

print(response.text)

如果您已经读到这里,如果您能阐明我在尝试登录网站并保持登录时做对或做错的事情,我将不胜感激。

最佳答案

根据 Andrew Cherevatkin 提到的,我应该使用 s.posts.get 而不是 requests。通过使用 requests.postrequests.get,我丢失了通过 session()

收集的所有内容

关于python 请求登录网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44689480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com