gpt4 book ai didi

python - 使用 python requests 模块登录基于 WordPress 的网站

转载 作者:行者123 更新时间:2023-11-30 22:42:46 31 4
gpt4 key购买 nike

我正在尝试使用 python 的请求模块和 beautifulsoup4 登录基于 Wordpress 的网站。看来代码无法成功登录。此外,网站上没有 csrf token 。如何成功登录网站?

import requests
import bs4 as bs
with requests.session() as c:
link="https://gpldl.com/sign-in/" #link of the webpage to be logged in
initial=c.get(link) #passing the get request

login_data={"log":"*****","pwd":"******"} #the login data from any account on the site. Stars must be replaced with username and password
page_login=c.post(link, data=login_data) #posting the login data into the link
print(page_login) #checking status of requested page
page=c.get("https://gpldl.com/my-gpldl-account/") #requesting source code of logged in page

good_data = bs.BeautifulSoup(page.content, "lxml") #parsing it with BS4
print(good_data.title) #printing this gives the title that is got from the page when accessed from an logged-out account

最佳答案

您将 POST 请求发送到错误的 URL,正确的 URL 应该是 https://gpldl.com/wp-login.php,而且有效负载有 5 个参数分别是logpwdremembermeredirect_toredirect_to_automatic

所以应该是:

login_data = {"log": "*****","pwd": "******", 
"rememberme": "forever",
"redirect_to": "https://gpldl.com/my-gpldl-account/",
"redirect_to_automatic": "1"
}

page_login = c.post('https://gpldl.com/wp-login.php', data=login_data)

编辑:

您可以使用 Chrome 开发工具在登录时查找所有这些信息,如下所示:

Login

至于rememberme键,我建议您做与浏览器完全相同的事情,还为您的请求添加一些 header ,特别是User-Agent,因为对于有些网站他们只是不欢迎您以这种方式登录。

关于python - 使用 python requests 模块登录基于 WordPress 的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42002336/

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