You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Requests登录WordPress网站失败问题求助

解决myforexfunds.com自动登录问题的方法

核心问题分析

  • WordPress的_wpnonce、_wp_http_referer这类字段是动态生成的,每次访问登录页面都会更新,硬写固定值必然失效。
  • 登录需要维持会话(Cookie),直接用requests.post不维护会话的话,服务器无法识别你的登录状态。
  • 你提供的代码存在语法错误:payload里的注释格式错误,导致brandnestor_action等字段未被正确加入;另外密码字段应为pwd而非pdw(符合WordPress默认登录字段规则)。

正确实现步骤

1. 用requests.Session维持会话

会话会自动保存和传递Cookie,这是登录成功的基础前提。

2. 先GET登录页面,提取动态字段

访问登录页面后,从HTML中解析出_wpnonce、brandnestor_action、_wp_http_referer这些必填的动态字段,推荐用BeautifulSoup做HTML解析。

3. 构造正确Payload并发送POST请求

将提取到的动态字段与账号密码一起放入payload,发送POST请求到登录接口。

完整代码示例

import requests
from bs4 import BeautifulSoup

# 初始化会话
session = requests.Session()

# 页面URL
login_url = "https://myforexfunds.com/login-signup/"
account_url = "https://myforexfunds.com/account-2"

# 第一步:GET登录页面,获取动态字段和Cookie
response = session.get(login_url)
soup = BeautifulSoup(response.text, "html.parser")

# 提取动态字段
wp_nonce = soup.find("input", {"name": "_wpnonce"})["value"]
brand_action = soup.find("input", {"name": "brandnestor_action"})["value"]
wp_referer = "/login-signup/"

# 构造Payload
payload = {
    "log": "你的邮箱账号",
    "pwd": "你的密码",
    "brandnestor_action": brand_action,
    "_wpnonce": wp_nonce,
    "_wp_http_referer": wp_referer
}

# 第二步:发送POST请求登录
login_response = session.post(login_url, data=payload)

# 验证登录状态:访问账号页面查看内容
account_response = session.get(account_url)
print(account_response.text)

额外注意事项

  • 先安装BeautifulSoup:pip install beautifulsoup4
  • 若网站有反爬机制,需添加请求头伪装浏览器:
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
    }
    # 在GET和POST请求时传入headers参数
    response = session.get(login_url, headers=headers)
    login_response = session.post(login_url, data=payload, headers=headers)
    
  • 避免频繁测试,防止账号被封禁。

内容的提问来源于stack exchange,提问作者pebs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 11:05:47