From ee89284a4c55065c4ab2b9aba40ecadc23975a9e Mon Sep 17 00:00:00 2001 From: YueL1331 <358700404@qq.com> Date: Fri, 16 Jan 2026 15:15:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E4=BB=A5=E5=8F=8A=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=BF=A1=E6=81=AF=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 拿取内容测试.py | 91 +++++++++++++++++++++++++++++++++++++++++++ 登录测试.py | 56 ++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 拿取内容测试.py create mode 100644 登录测试.py diff --git a/拿取内容测试.py b/拿取内容测试.py new file mode 100644 index 0000000..01a21e3 --- /dev/null +++ b/拿取内容测试.py @@ -0,0 +1,91 @@ +import requests +import json +import os + +# ================= 配置区域 ================= +base_url = "http://111.198.24.44:88/index.php" + +# 1. 登录信息 +login_payload = { + "module": "Users", + "action": "Authenticate", + "return_module": "Users", + "return_action": "Login", + "user_name": "你的用户名", # <--- 记得填 + "user_password": "你的密码", # <--- 记得填 + "login_theme": "newskin" +} + +# 2. 抓取数据参数 (保留了你之前的筛选条件) +data_payload = { + "module": "SalesOrder", + "action": "SalesOrderAjax", + "file": "ListViewData", + "sorder": "", + "start": "1", + "pagesize": "100", + "actionId": "1768546984243", + "isFilter": "true", + "search[viewscope]": "all_to_me", + "search[viewname]": "324126", + "filter[Fields0]": "subject", + "filter[Condition0]": "cts", + "filter[Srch_value0]": "W25A", + "filter[type0]": "text", + "filter[dateCondition1]": "prevfy", + "filter[Fields1]": "duedate", + "filter[Condition1]": "btwa", + "filter[Srch_value1]": "2025-01-01,2025-12-31", + "filter[type1]": "date", + "filter[Fields2]": "subject", + "filter[Condition2]": "dcts", + "filter[Srch_value2]": "取消", + "filter[type2]": "text", + "filter[search_cnt]": "3", + "filter[matchtype]": "all" +} + +headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", + "Referer": "http://111.198.24.44:88/index.php?module=SalesOrder&action=index" +} + +# ================= 执行逻辑 ================= +session = requests.Session() + +try: + print("1. 正在登录...") + session.post(base_url, data=login_payload, headers=headers) + + if 'PHPSESSID' in session.cookies: + print(" 登录成功,Cookie已获取。") + else: + print(" ⚠️ 警告:可能登录失败 (未检测到PHPSESSID)。") + + print("2. 正在获取数据并导出...") + resp = session.post(base_url, data=data_payload, headers=headers) + + # === 关键修改:保存文件 === + try: + # 尝试解析 JSON + json_data = resp.json() + + # 定义文件名 + filename = "result.json" + + # 写入文件 + # ensure_ascii=False 保证中文能正常显示,而不是显示成 \u53d6\u6d88 + with open(filename, 'w', encoding='utf-8') as f: + json.dump(json_data, f, ensure_ascii=False, indent=4) + + print(f"\n✅ 成功!数据已保存到当前目录下的: 【{filename}】") + print(f" 文件路径: {os.path.abspath(filename)}") + + except json.JSONDecodeError: + print("\n❌ 失败:服务器返回的不是 JSON 格式。") + print("可能是 HTML 页面,已保存为 'error_page.html' 供检查。") + with open("error_page.html", "w", encoding="utf-8") as f: + f.write(resp.text) + +except Exception as e: + print(f"发生错误: {e}") \ No newline at end of file diff --git a/登录测试.py b/登录测试.py new file mode 100644 index 0000000..b53e7f3 --- /dev/null +++ b/登录测试.py @@ -0,0 +1,56 @@ +import requests + +# 1. 准备登录信息 +login_url = "http://111.198.24.44:88/index.php" + +# 这是你刚刚抓到的 Payload 数据 +payload = { + "error": "", + "login_theme": "newskin", + "module": "Users", + "action": "Authenticate", + "return_module": "Users", + "return_action": "Login", + "user_name": "TEST", # 在这里填入真实的用户名 + "user_password": "test", # 在这里填入真实的密码 + "code": "", + "user_validate": "" +} + +# 伪装成浏览器(这很重要,防止被反爬虫拦截) +headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" +} + +# 2. 创建一个 Session (会话) +# Session 的作用就像一个浏览器窗口,它会自动保存 Cookie +session = requests.Session() + +try: + # 3. 发送登录请求 + # allow_redirects=True 会自动跟随 301 跳转到主页,就像浏览器一样 + response = session.post(login_url, data=payload, headers=headers, allow_redirects=True) + + # 4. 检查结果 + print(f"状态码: {response.status_code}") + + # 获取到的 Cookie + print("获取到的 Cookies:") + print(session.cookies.get_dict()) + + # 简单的验证:如果返回的网页里包含了'退出'或用户名的字样,说明登录成功了 + if "logout" in response.text.lower() or "退出" in response.text: + print("\n==> 登录成功! <==") + + # 【进阶】: 登录成功后,你可以直接用这个 session 访问其他页面 + # 比如访问主页获取数据,它会自动带上刚才拿到的 cookie + # home_page = session.get("http://111.198.24.44:88/index.php?module=Home&action=index") + # print(home_page.text[:200]) + + else: + print("\n可能登录失败,请检查用户名密码。") + # 如果失败,打印一部分返回内容看看原因 + print("返回内容预览:", response.text[:500]) + +except Exception as e: + print(f"发生错误: {e}") \ No newline at end of file