107 lines
3.6 KiB
Python
107 lines
3.6 KiB
Python
import requests
|
||
import json
|
||
import urllib.parse
|
||
|
||
# ================= 配置区域 =================
|
||
# 登录 URL
|
||
BASE_URL = "http://111.198.24.44:88/index.php"
|
||
|
||
# 用户名和密码 (请填入真实信息)
|
||
USERNAME = "TEST"
|
||
PASSWORD = "test" # <--- 请在这里填入真实密码
|
||
|
||
# 伪装 Header
|
||
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",
|
||
"X-Requested-With": "XMLHttpRequest" # AJAX 请求通常需要这个头
|
||
}
|
||
|
||
|
||
# ===========================================
|
||
|
||
def main():
|
||
# 1. 创建 Session 对象 (它会自动管理 Cookies)
|
||
session = requests.Session()
|
||
|
||
# 2. 准备登录数据
|
||
login_payload = {
|
||
"error": "",
|
||
"login_theme": "newskin",
|
||
"module": "Users",
|
||
"action": "Authenticate",
|
||
"return_module": "Users",
|
||
"return_action": "Login",
|
||
"user_name": USERNAME,
|
||
"user_password": PASSWORD,
|
||
"code": "",
|
||
"user_validate": ""
|
||
}
|
||
|
||
print("1. 正在尝试登录...")
|
||
try:
|
||
# 发送登录请求
|
||
login_resp = session.post(BASE_URL, data=login_payload, headers=headers)
|
||
|
||
# 简单检查登录是否成功 (根据你提供的逻辑)
|
||
if "logout" not in login_resp.text.lower() and "退出" not in login_resp.text:
|
||
print(f"[-] 登录失败,状态码: {login_resp.status_code}")
|
||
# print(login_resp.text[:500]) # 调试用
|
||
return
|
||
|
||
print("[+] 登录成功!")
|
||
|
||
# 3. 准备获取数据的 Payload
|
||
# 注意:这里将 URL 参数转换为了字典
|
||
# requests 会自动处理 urlencode,所以 '否' 不需要写成 '%E5%90%A6'
|
||
data_payload = {
|
||
"module": "Products",
|
||
"action": "ProductsAjax",
|
||
"file": "ListViewData",
|
||
"sorder": "",
|
||
"start": "1",
|
||
"order_by": "",
|
||
"pagesize": "100",
|
||
"actionId": "1768981966230",
|
||
"isFilter": "true",
|
||
"search[viewname]": "28",
|
||
"filter[Fields0]": "cf_2318",
|
||
"filter[Condition0]": "is",
|
||
"filter[Srch_value0]": "否", # 对应 %E5%90%A6
|
||
"filter[type0]": "opts",
|
||
"filter[search_cnt]": "1",
|
||
"filter[matchtype]": "all"
|
||
}
|
||
|
||
print("2. 正在获取列表数据...")
|
||
|
||
# 发送数据请求
|
||
# 注意:通常这种 Ajax 列表查询也是 POST 请求,如果失败可以尝试 session.get(..., params=data_payload)
|
||
data_resp = session.post(BASE_URL, data=data_payload, headers=headers)
|
||
|
||
print(f" 状态码: {data_resp.status_code}")
|
||
|
||
# 4. 解析并输出 JSON
|
||
try:
|
||
# 尝试解析 JSON
|
||
json_data = data_resp.json()
|
||
|
||
# 在控制台漂亮地打印出来
|
||
print("\n[+] 获取数据成功,JSON 内容预览 (前500字符):")
|
||
print(json.dumps(json_data, ensure_ascii=False, indent=4)[:500] + "...")
|
||
|
||
# 将完整 JSON 保存到文件
|
||
with open("result.json", "w", encoding="utf-8") as f:
|
||
json.dump(json_data, f, ensure_ascii=False, indent=4)
|
||
print("\n[+]完整数据已保存至当前目录下的 result.json 文件")
|
||
|
||
except json.JSONDecodeError:
|
||
print("[-] 返回的不是有效的 JSON 数据。")
|
||
print("可能原因:1. Session 过期 2. 参数错误 3. 服务器报错")
|
||
print("返回内容片段:", data_resp.text[:500])
|
||
|
||
except Exception as e:
|
||
print(f"发生错误: {e}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main() |