Spaces:
Sleeping
Sleeping
Update Wright.py
Browse files
Wright.py
CHANGED
|
@@ -1,412 +1,412 @@
|
|
| 1 |
-
from playwright.sync_api import sync_playwright
|
| 2 |
-
import time
|
| 3 |
-
from typing import Optional
|
| 4 |
-
import asyncio
|
| 5 |
-
from sympy import false
|
| 6 |
-
from win32gui import FlashWindowEx
|
| 7 |
-
import json
|
| 8 |
-
import requests
|
| 9 |
-
import os
|
| 10 |
-
def automate_cwall_login(
|
| 11 |
-
username: str,
|
| 12 |
-
password: str,
|
| 13 |
-
headless: bool = False,
|
| 14 |
-
timeout: int = 30000,
|
| 15 |
-
keep_open: bool = False
|
| 16 |
-
):
|
| 17 |
-
"""
|
| 18 |
-
自动化登录cwall网站的函数
|
| 19 |
-
|
| 20 |
-
参数:
|
| 21 |
-
username (str): 用户名
|
| 22 |
-
password (str): 密码
|
| 23 |
-
headless (bool): 是否无头模式,默认显示浏览器
|
| 24 |
-
timeout (int): 超时时间(毫秒),默认30秒
|
| 25 |
-
keep_open (bool): 完成后是否保持浏览器打开
|
| 26 |
-
"""
|
| 27 |
-
with sync_playwright() as p:
|
| 28 |
-
browser = p.chromium.launch(headless=headless)
|
| 29 |
-
context = browser.new_context()
|
| 30 |
-
page = context.new_page()
|
| 31 |
-
|
| 32 |
-
try:
|
| 33 |
-
# 设置默认超时
|
| 34 |
-
page.set_default_timeout(timeout)
|
| 35 |
-
|
| 36 |
-
print("正在导航到登录页面...")
|
| 37 |
-
page.goto("https://cwall.czoffice.top/login.php", wait_until="networkidle")
|
| 38 |
-
|
| 39 |
-
# 输入凭据
|
| 40 |
-
print("正在输入用户名和密码...")
|
| 41 |
-
page.fill("xpath=/html/body/div[2]/form/div[1]/input", username)
|
| 42 |
-
page.fill("xpath=/html/body/div[2]/form/div[2]/input", password)
|
| 43 |
-
|
| 44 |
-
# 点击登录
|
| 45 |
-
print("正在提交登录表单...")
|
| 46 |
-
page.click("xpath=/html/body/div[2]/form/input[@type='submit']")
|
| 47 |
-
|
| 48 |
-
# 等待登录完成
|
| 49 |
-
print("等待登录完成...")
|
| 50 |
-
try:
|
| 51 |
-
# 尝试检测登录成功元素
|
| 52 |
-
page.wait_for_selector("xpath=//*[contains(text(),'欢迎') or contains(text(),'Dashboard')]", timeout=5000)
|
| 53 |
-
print("登录成功!")
|
| 54 |
-
except:
|
| 55 |
-
print("未检测到欢迎信息,但可能已登录成功")
|
| 56 |
-
|
| 57 |
-
if keep_open:
|
| 58 |
-
print(f"浏览器将保持打开,按Ctrl+C终止程序...")
|
| 59 |
-
while True:
|
| 60 |
-
time.sleep(1)
|
| 61 |
-
context.storage_state(path="auth.json")
|
| 62 |
-
|
| 63 |
-
return True
|
| 64 |
-
|
| 65 |
-
except Exception as e:
|
| 66 |
-
print(f"自动化过程中发生错误: {str(e)}")
|
| 67 |
-
# 出错时截图保存
|
| 68 |
-
page.screenshot(path="error_screenshot.png")
|
| 69 |
-
print("已保存错误截图: error_screenshot.png")
|
| 70 |
-
return False
|
| 71 |
-
|
| 72 |
-
finally:
|
| 73 |
-
if not keep_open:
|
| 74 |
-
browser.close()
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
def cwall_ww_sub(
|
| 78 |
-
info: str,
|
| 79 |
-
kind: str,
|
| 80 |
-
kind_enable: bool = False,
|
| 81 |
-
image_update_enable: bool = False,
|
| 82 |
-
image: Optional[str] = None,
|
| 83 |
-
headless: bool = False,
|
| 84 |
-
timeout: int = 30000,
|
| 85 |
-
keep_open: bool = False
|
| 86 |
-
) -> bool:
|
| 87 |
-
"""
|
| 88 |
-
Cwall WW自动发送参数
|
| 89 |
-
|
| 90 |
-
参数:
|
| 91 |
-
info (str): 发送内容
|
| 92 |
-
kind_enable (bool): 是否启用分类
|
| 93 |
-
kind (str): 分类(只支持一个)
|
| 94 |
-
image_update_enable (bool): 是否启用图片上传
|
| 95 |
-
image (str): 图片路径
|
| 96 |
-
headless (bool): 是否无头模式,默认显示浏览器
|
| 97 |
-
timeout (int): 超时时间(毫秒),默认30秒
|
| 98 |
-
keep_open (bool): 完成后是否保持浏览器打开
|
| 99 |
-
"""
|
| 100 |
-
with sync_playwright() as p:
|
| 101 |
-
browser = p.chromium.launch(headless=headless)
|
| 102 |
-
|
| 103 |
-
context = browser.new_context(storage_state="auth.json")
|
| 104 |
-
page = context.new_page()
|
| 105 |
-
|
| 106 |
-
try:
|
| 107 |
-
page.set_default_timeout(timeout)
|
| 108 |
-
|
| 109 |
-
print("正在导航ww页面...")
|
| 110 |
-
page.goto("https://cwall.czoffice.top/ww/", wait_until="networkidle")
|
| 111 |
-
|
| 112 |
-
# 输入内容
|
| 113 |
-
print("正在输入ww内容...")
|
| 114 |
-
page.fill("xpath=/html/body/div[3]/textarea", info)
|
| 115 |
-
|
| 116 |
-
# 处理分类
|
| 117 |
-
if kind_enable:
|
| 118 |
-
print(f"正在设置分类: {kind}")
|
| 119 |
-
page.fill("xpath=/html/body/div[3]/input", kind)
|
| 120 |
-
page.press("xpath=/html/body/div[3]/input", "Enter")
|
| 121 |
-
|
| 122 |
-
# 处理图片上传
|
| 123 |
-
if image_update_enable and image:
|
| 124 |
-
print(f"正在上传图片: {image}")
|
| 125 |
-
# 先触发文件选择对话框的打开(如点击上传按钮)
|
| 126 |
-
# page.click("xpath=/html/body/div[3]/button[1]")
|
| 127 |
-
#
|
| 128 |
-
# # 然后等待文件输入框出现并上传
|
| 129 |
-
# with page.expect_file_chooser() as fc:
|
| 130 |
-
# page.click("text=打开(O)") # 触发文件选择器的元素
|
| 131 |
-
# file_chooser = fc.value
|
| 132 |
-
# file_chooser.set_files(image)
|
| 133 |
-
with page.expect_file_chooser() as fc_info:
|
| 134 |
-
page.get_by_role("button", name="上传图片").click()
|
| 135 |
-
file_chooser = fc_info.value
|
| 136 |
-
file_chooser.set_files(image)
|
| 137 |
-
|
| 138 |
-
# 提交表单
|
| 139 |
-
print("正在提交内容...")
|
| 140 |
-
page.click("xpath=/html/body/div[3]/button[2]")
|
| 141 |
-
time.sleep(3)
|
| 142 |
-
# 等待提交完成
|
| 143 |
-
# page.wait_for_selector("text=提交成功", timeout=15000)
|
| 144 |
-
# print("提交成功!")
|
| 145 |
-
|
| 146 |
-
if keep_open:
|
| 147 |
-
print("浏览器将保持打开,按Ctrl+C退出...")
|
| 148 |
-
while True:
|
| 149 |
-
time.sleep(1)
|
| 150 |
-
|
| 151 |
-
return True
|
| 152 |
-
|
| 153 |
-
except Exception as e:
|
| 154 |
-
print(f"操作失败: {str(e)}")
|
| 155 |
-
page.screenshot(path="ww_submission_error.png")
|
| 156 |
-
print("错误截图已保存为 ww_submission_error.png")
|
| 157 |
-
return False
|
| 158 |
-
|
| 159 |
-
finally:
|
| 160 |
-
if not keep_open:
|
| 161 |
-
browser.close()
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
def Get_PHPSESSID(
|
| 165 |
-
username: str,
|
| 166 |
-
password: str,
|
| 167 |
-
headless: bool = False,
|
| 168 |
-
timeout: int = 30000,
|
| 169 |
-
keep_open: bool = False
|
| 170 |
-
):
|
| 171 |
-
"""
|
| 172 |
-
自动化获得PHPSESSID的函数
|
| 173 |
-
|
| 174 |
-
参数:
|
| 175 |
-
username (str): 用户名
|
| 176 |
-
password (str): 密码
|
| 177 |
-
headless (bool): 是否无头模式,默认显示浏览器
|
| 178 |
-
timeout (int): 超时时间(毫秒),默认30秒
|
| 179 |
-
keep_open (bool): 完成后是否保持浏览器打开
|
| 180 |
-
"""
|
| 181 |
-
with sync_playwright() as p:
|
| 182 |
-
browser = p.chromium.launch(headless=headless)
|
| 183 |
-
context = browser.new_context()
|
| 184 |
-
page = context.new_page()
|
| 185 |
-
|
| 186 |
-
try:
|
| 187 |
-
# 设置默认超时
|
| 188 |
-
page.set_default_timeout(timeout)
|
| 189 |
-
|
| 190 |
-
print("正在导航到登录页面...")
|
| 191 |
-
page.goto("https://cwall.czoffice.top/login.php", wait_until="networkidle")
|
| 192 |
-
|
| 193 |
-
# 输入凭据
|
| 194 |
-
print("正在输入用户名和密码...")
|
| 195 |
-
page.fill("xpath=/html/body/div[2]/form/div[1]/input", username)
|
| 196 |
-
page.fill("xpath=/html/body/div[2]/form/div[2]/input", password)
|
| 197 |
-
|
| 198 |
-
# 点击登录
|
| 199 |
-
print("正在提交登录表单...")
|
| 200 |
-
page.click("xpath=/html/body/div[2]/form/input[@type='submit']")
|
| 201 |
-
|
| 202 |
-
# 等待登录完成
|
| 203 |
-
print("等待登录完成...")
|
| 204 |
-
try:
|
| 205 |
-
# 尝试检测登录成功元素
|
| 206 |
-
page.wait_for_selector("xpath=//*[contains(text(),'欢迎') or contains(text(),'Dashboard')]",
|
| 207 |
-
timeout=5000)
|
| 208 |
-
print("登录成功!")
|
| 209 |
-
except:
|
| 210 |
-
print("未检测到欢迎信息,但可能已登录成功")
|
| 211 |
-
|
| 212 |
-
if keep_open:
|
| 213 |
-
print(f"浏览器将保持打开,按Ctrl+C终止程序...")
|
| 214 |
-
while True:
|
| 215 |
-
time.sleep(1)
|
| 216 |
-
context.storage_state(path="auth.json")
|
| 217 |
-
# 读取auth.json文件
|
| 218 |
-
with open('auth.json', 'r', encoding='utf-8') as file:
|
| 219 |
-
data = json.load(file)
|
| 220 |
-
|
| 221 |
-
# 提取PHPSESSID
|
| 222 |
-
phpsessid = None
|
| 223 |
-
for cookie in data['cookies']:
|
| 224 |
-
if cookie['name'] == 'PHPSESSID':
|
| 225 |
-
phpsessid = cookie['value']
|
| 226 |
-
break
|
| 227 |
-
|
| 228 |
-
if phpsessid:
|
| 229 |
-
print("提取到的PHPSESSID是:", phpsessid)
|
| 230 |
-
else:
|
| 231 |
-
print("未找到PHPSESSID")
|
| 232 |
-
return True
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
except Exception as e:
|
| 236 |
-
print(f"自动化过程中发生错误: {str(e)}")
|
| 237 |
-
# 出错时截图保存
|
| 238 |
-
page.screenshot(path="error_screenshot.png")
|
| 239 |
-
print("已保存错误截图: error_screenshot.png")
|
| 240 |
-
return False
|
| 241 |
-
|
| 242 |
-
finally:
|
| 243 |
-
if not keep_open:
|
| 244 |
-
browser.close()
|
| 245 |
-
def get_cwall_ww_info(
|
| 246 |
-
page: str = "1"
|
| 247 |
-
):
|
| 248 |
-
"""
|
| 249 |
-
自动化解析Cwall ww内容
|
| 250 |
-
|
| 251 |
-
参数:
|
| 252 |
-
page (str): 页码
|
| 253 |
-
返回:
|
| 254 |
-
list: 包含所有ww信息的列表,每个元素是一个字典
|
| 255 |
-
"""
|
| 256 |
-
result = []
|
| 257 |
-
#请求Cwall API
|
| 258 |
-
response = requests.get("https://cwall.czoffice.top/ww/fetch_contents.php?page="+page)
|
| 259 |
-
# 将响应内容解析为 Python 字典
|
| 260 |
-
if response.status_code == 200:
|
| 261 |
-
data_dict = response.json()
|
| 262 |
-
|
| 263 |
-
# 检查请求是否成功
|
| 264 |
-
if data_dict.get('success'):
|
| 265 |
-
# 遍历 data 列表
|
| 266 |
-
for item in data_dict.get('data', []):
|
| 267 |
-
result.append({
|
| 268 |
-
"ID": item.get('id'),
|
| 269 |
-
"UserID": item.get('user_id'),
|
| 270 |
-
"用户名": item.get('username'),
|
| 271 |
-
"内容": item.get('content'),
|
| 272 |
-
"发布时间": item.get('publish_time'),
|
| 273 |
-
"点赞数": item.get('like'),
|
| 274 |
-
"图片列表": item.get('images')
|
| 275 |
-
})
|
| 276 |
-
return result
|
| 277 |
-
else:
|
| 278 |
-
print("请求
|
| 279 |
-
return None
|
| 280 |
-
else:
|
| 281 |
-
print(f"请求失败,状态码: {response.status_code}")
|
| 282 |
-
return None
|
| 283 |
-
def clear_auth():
|
| 284 |
-
if os.path.exists("auth.json"):
|
| 285 |
-
os.remove("auth.json")
|
| 286 |
-
print("auth.json已删除")
|
| 287 |
-
else:
|
| 288 |
-
print("auth.json不存在")
|
| 289 |
-
def ww_like(
|
| 290 |
-
post_id: str,
|
| 291 |
-
headless: bool = False,
|
| 292 |
-
timeout: int = 30000,
|
| 293 |
-
keep_open: bool = False
|
| 294 |
-
):
|
| 295 |
-
"""
|
| 296 |
-
自动化ww点赞/取消
|
| 297 |
-
|
| 298 |
-
参数:
|
| 299 |
-
Post_id(str): ww的ID
|
| 300 |
-
headless (bool): 是否无头模式,默认显示浏览器
|
| 301 |
-
timeout (int): 超时时间(毫秒),默认30秒
|
| 302 |
-
keep_open (bool): 完成后是否保持浏
|
| 303 |
-
"""
|
| 304 |
-
with sync_playwright() as p:
|
| 305 |
-
browser = p.chromium.launch(headless=headless)
|
| 306 |
-
context = browser.new_context(storage_state="auth.json")
|
| 307 |
-
page = context.new_page()
|
| 308 |
-
try:
|
| 309 |
-
page.set_default_timeout(timeout)
|
| 310 |
-
print("正在导航ww页面...")
|
| 311 |
-
page.goto("https://cwall.czoffice.top/ww/detail.php?id="+post_id)
|
| 312 |
-
|
| 313 |
-
#点击点赞按钮
|
| 314 |
-
print("正在点赞")
|
| 315 |
-
page.click("xpath=/html/body/div/div[1]/button[1]")
|
| 316 |
-
time.sleep(1)
|
| 317 |
-
if keep_open:
|
| 318 |
-
print(f"浏览器将保持打开,按Ctrl+C终止程序...")
|
| 319 |
-
while True:
|
| 320 |
-
time.sleep(1)
|
| 321 |
-
return True
|
| 322 |
-
except Exception as e:
|
| 323 |
-
print("自动化过程出现错误")
|
| 324 |
-
page.screenshot(path="error-like.png")
|
| 325 |
-
print("已保存错误截图:error-like.png")
|
| 326 |
-
return False
|
| 327 |
-
finally:
|
| 328 |
-
if not keep_open:
|
| 329 |
-
browser.close()
|
| 330 |
-
def ww_Comment(
|
| 331 |
-
info: str,
|
| 332 |
-
post_id: str,
|
| 333 |
-
headless: bool = False,
|
| 334 |
-
timeout: int = 30000,
|
| 335 |
-
keep_open: bool = False
|
| 336 |
-
):
|
| 337 |
-
"""
|
| 338 |
-
自动化ww评论
|
| 339 |
-
参数:
|
| 340 |
-
info(str): 评论内容
|
| 341 |
-
Post_id(str): ww的ID
|
| 342 |
-
headless (bool): 是否无头模式,默认显示浏览器
|
| 343 |
-
timeout (int): 超时时间(毫秒),默认30秒
|
| 344 |
-
keep_open (bool): 完成后是否保持浏览器打开
|
| 345 |
-
"""
|
| 346 |
-
with sync_playwright() as p:
|
| 347 |
-
browser = p.chromium.launch(headless=headless)
|
| 348 |
-
context = browser.new_context(storage_state="auth.json")
|
| 349 |
-
page = context.new_page()
|
| 350 |
-
try:
|
| 351 |
-
page.set_default_timeout(timeout)
|
| 352 |
-
print("正在导航ww页面...")
|
| 353 |
-
page.goto("https://cwall.czoffice.top/ww/detail.php?id=" + post_id)
|
| 354 |
-
# 输入
|
| 355 |
-
page.fill("xpath=/html/body/div/form/textarea",info)
|
| 356 |
-
#提交
|
| 357 |
-
page.click("xpath=/html/body/div/form/button")
|
| 358 |
-
if keep_open:
|
| 359 |
-
print(f"浏览器将保持打开,按Ctrl+C终止程序...")
|
| 360 |
-
while True:
|
| 361 |
-
time.sleep(1)
|
| 362 |
-
return True
|
| 363 |
-
except Exception as e:
|
| 364 |
-
print("自动化过程出现错误")
|
| 365 |
-
page.screenshot(path="error-comment.png")
|
| 366 |
-
print("已保存错误截图:error-comment.png")
|
| 367 |
-
return False
|
| 368 |
-
finally:
|
| 369 |
-
if not keep_open:
|
| 370 |
-
browser.close()
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
# 使用示例
|
| 374 |
-
if __name__ == "__main__":
|
| 375 |
-
# # 基本用法
|
| 376 |
-
# automate_cwall_login(
|
| 377 |
-
# username="一只fish (づ ●─● )づ",
|
| 378 |
-
# password="zy842530",
|
| 379 |
-
# headless=False
|
| 380 |
-
# )
|
| 381 |
-
# cwall_ww_sub(
|
| 382 |
-
# info="Xes什么私人UI,这个C拉的比面条还长", # 必需
|
| 383 |
-
# kind="Open Cwall API", # 必需
|
| 384 |
-
# kind_enable=True,
|
| 385 |
-
# image_update_enable=True,
|
| 386 |
-
# image="C://Users/张雨.WIN-KF4D01O81O2/desktop/屏幕截图 2025-05-17 185324.png", # 可选
|
| 387 |
-
# headless=True,
|
| 388 |
-
# keep_open=False
|
| 389 |
-
# )
|
| 390 |
-
|
| 391 |
-
# 高级用法 - 保持浏览器打开
|
| 392 |
-
# automate_cwall_login(
|
| 393 |
-
# username="testuser",
|
| 394 |
-
# password="testpass",
|
| 395 |
-
# keep_open=True
|
| 396 |
-
# )
|
| 397 |
-
# get_cwall_ww_info(
|
| 398 |
-
# page= "1"
|
| 399 |
-
# )
|
| 400 |
-
# clear_auth()
|
| 401 |
-
# ww_like(
|
| 402 |
-
# post_id="3432",
|
| 403 |
-
# keep_open = True,
|
| 404 |
-
# timeout=30000,
|
| 405 |
-
# )
|
| 406 |
-
ww_Comment(
|
| 407 |
-
info="OpenCwallAPI评论测试", # 必需
|
| 408 |
-
post_id="3438", # 必需
|
| 409 |
-
headless=False,
|
| 410 |
-
keep_open=False,
|
| 411 |
-
timeout=30000,
|
| 412 |
)
|
|
|
|
| 1 |
+
from playwright.sync_api import sync_playwright
|
| 2 |
+
import time
|
| 3 |
+
from typing import Optional
|
| 4 |
+
import asyncio
|
| 5 |
+
from sympy import false
|
| 6 |
+
# from win32gui import FlashWindowEx
|
| 7 |
+
import json
|
| 8 |
+
import requests
|
| 9 |
+
import os
|
| 10 |
+
def automate_cwall_login(
|
| 11 |
+
username: str,
|
| 12 |
+
password: str,
|
| 13 |
+
headless: bool = False,
|
| 14 |
+
timeout: int = 30000,
|
| 15 |
+
keep_open: bool = False
|
| 16 |
+
):
|
| 17 |
+
"""
|
| 18 |
+
自动化登录cwall网站的函数
|
| 19 |
+
|
| 20 |
+
参数:
|
| 21 |
+
username (str): 用户名
|
| 22 |
+
password (str): 密码
|
| 23 |
+
headless (bool): 是否无头模式,默认显示浏览器
|
| 24 |
+
timeout (int): 超时时间(毫秒),默认30秒
|
| 25 |
+
keep_open (bool): 完成后是否保持浏览器打开
|
| 26 |
+
"""
|
| 27 |
+
with sync_playwright() as p:
|
| 28 |
+
browser = p.chromium.launch(headless=headless)
|
| 29 |
+
context = browser.new_context()
|
| 30 |
+
page = context.new_page()
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# 设置默认超时
|
| 34 |
+
page.set_default_timeout(timeout)
|
| 35 |
+
|
| 36 |
+
print("正在导航到登录页面...")
|
| 37 |
+
page.goto("https://cwall.czoffice.top/login.php", wait_until="networkidle")
|
| 38 |
+
|
| 39 |
+
# 输入凭据
|
| 40 |
+
print("正在输入用户名和密码...")
|
| 41 |
+
page.fill("xpath=/html/body/div[2]/form/div[1]/input", username)
|
| 42 |
+
page.fill("xpath=/html/body/div[2]/form/div[2]/input", password)
|
| 43 |
+
|
| 44 |
+
# 点击登录
|
| 45 |
+
print("正在提交登录表单...")
|
| 46 |
+
page.click("xpath=/html/body/div[2]/form/input[@type='submit']")
|
| 47 |
+
|
| 48 |
+
# 等待登录完成
|
| 49 |
+
print("等待登录完成...")
|
| 50 |
+
try:
|
| 51 |
+
# 尝试检测登录成功元素
|
| 52 |
+
page.wait_for_selector("xpath=//*[contains(text(),'欢迎') or contains(text(),'Dashboard')]", timeout=5000)
|
| 53 |
+
print("登录成功!")
|
| 54 |
+
except:
|
| 55 |
+
print("未检测到欢迎信息,但可能已登录成功")
|
| 56 |
+
|
| 57 |
+
if keep_open:
|
| 58 |
+
print(f"浏览器将保持打开,按Ctrl+C终止程序...")
|
| 59 |
+
while True:
|
| 60 |
+
time.sleep(1)
|
| 61 |
+
context.storage_state(path="auth.json")
|
| 62 |
+
|
| 63 |
+
return True
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"自动化过程中发生错误: {str(e)}")
|
| 67 |
+
# 出错时截图保存
|
| 68 |
+
page.screenshot(path="error_screenshot.png")
|
| 69 |
+
print("已保存错误截图: error_screenshot.png")
|
| 70 |
+
return False
|
| 71 |
+
|
| 72 |
+
finally:
|
| 73 |
+
if not keep_open:
|
| 74 |
+
browser.close()
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def cwall_ww_sub(
|
| 78 |
+
info: str,
|
| 79 |
+
kind: str,
|
| 80 |
+
kind_enable: bool = False,
|
| 81 |
+
image_update_enable: bool = False,
|
| 82 |
+
image: Optional[str] = None,
|
| 83 |
+
headless: bool = False,
|
| 84 |
+
timeout: int = 30000,
|
| 85 |
+
keep_open: bool = False
|
| 86 |
+
) -> bool:
|
| 87 |
+
"""
|
| 88 |
+
Cwall WW自动发送参数
|
| 89 |
+
|
| 90 |
+
参数:
|
| 91 |
+
info (str): 发送内容
|
| 92 |
+
kind_enable (bool): 是否启用分类
|
| 93 |
+
kind (str): 分类(只支持一个)
|
| 94 |
+
image_update_enable (bool): 是否启用图片上传
|
| 95 |
+
image (str): 图片路径
|
| 96 |
+
headless (bool): 是否无头模式,默认显示浏览器
|
| 97 |
+
timeout (int): 超时时间(毫秒),默认30秒
|
| 98 |
+
keep_open (bool): 完成后是否保持浏览器打开
|
| 99 |
+
"""
|
| 100 |
+
with sync_playwright() as p:
|
| 101 |
+
browser = p.chromium.launch(headless=headless)
|
| 102 |
+
|
| 103 |
+
context = browser.new_context(storage_state="auth.json")
|
| 104 |
+
page = context.new_page()
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
page.set_default_timeout(timeout)
|
| 108 |
+
|
| 109 |
+
print("正在导航ww页面...")
|
| 110 |
+
page.goto("https://cwall.czoffice.top/ww/", wait_until="networkidle")
|
| 111 |
+
|
| 112 |
+
# 输入内容
|
| 113 |
+
print("正在输入ww内容...")
|
| 114 |
+
page.fill("xpath=/html/body/div[3]/textarea", info)
|
| 115 |
+
|
| 116 |
+
# 处理分类
|
| 117 |
+
if kind_enable:
|
| 118 |
+
print(f"正在设置分类: {kind}")
|
| 119 |
+
page.fill("xpath=/html/body/div[3]/input", kind)
|
| 120 |
+
page.press("xpath=/html/body/div[3]/input", "Enter")
|
| 121 |
+
|
| 122 |
+
# 处理图片上传
|
| 123 |
+
if image_update_enable and image:
|
| 124 |
+
print(f"正在上传图片: {image}")
|
| 125 |
+
# 先触发文件选择对话框的打开(如点击上传按钮)
|
| 126 |
+
# page.click("xpath=/html/body/div[3]/button[1]")
|
| 127 |
+
#
|
| 128 |
+
# # 然后等待文件输入框出现并上传
|
| 129 |
+
# with page.expect_file_chooser() as fc:
|
| 130 |
+
# page.click("text=打开(O)") # 触发文件选择器的元素
|
| 131 |
+
# file_chooser = fc.value
|
| 132 |
+
# file_chooser.set_files(image)
|
| 133 |
+
with page.expect_file_chooser() as fc_info:
|
| 134 |
+
page.get_by_role("button", name="上传图片").click()
|
| 135 |
+
file_chooser = fc_info.value
|
| 136 |
+
file_chooser.set_files(image)
|
| 137 |
+
|
| 138 |
+
# 提交表单
|
| 139 |
+
print("正在提交内容...")
|
| 140 |
+
page.click("xpath=/html/body/div[3]/button[2]")
|
| 141 |
+
time.sleep(3)
|
| 142 |
+
# 等待提交完成
|
| 143 |
+
# page.wait_for_selector("text=提交成功", timeout=15000)
|
| 144 |
+
# print("提交成功!")
|
| 145 |
+
|
| 146 |
+
if keep_open:
|
| 147 |
+
print("浏览器将保持打开,按Ctrl+C退出...")
|
| 148 |
+
while True:
|
| 149 |
+
time.sleep(1)
|
| 150 |
+
|
| 151 |
+
return True
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
print(f"操作失败: {str(e)}")
|
| 155 |
+
page.screenshot(path="ww_submission_error.png")
|
| 156 |
+
print("错误截图已保存为 ww_submission_error.png")
|
| 157 |
+
return False
|
| 158 |
+
|
| 159 |
+
finally:
|
| 160 |
+
if not keep_open:
|
| 161 |
+
browser.close()
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def Get_PHPSESSID(
|
| 165 |
+
username: str,
|
| 166 |
+
password: str,
|
| 167 |
+
headless: bool = False,
|
| 168 |
+
timeout: int = 30000,
|
| 169 |
+
keep_open: bool = False
|
| 170 |
+
):
|
| 171 |
+
"""
|
| 172 |
+
自动化获得PHPSESSID的函数
|
| 173 |
+
|
| 174 |
+
参数:
|
| 175 |
+
username (str): 用户名
|
| 176 |
+
password (str): 密码
|
| 177 |
+
headless (bool): 是否无头模式,默认显示浏览器
|
| 178 |
+
timeout (int): 超时时间(毫秒),默认30秒
|
| 179 |
+
keep_open (bool): 完成后是否保持浏览器打开
|
| 180 |
+
"""
|
| 181 |
+
with sync_playwright() as p:
|
| 182 |
+
browser = p.chromium.launch(headless=headless)
|
| 183 |
+
context = browser.new_context()
|
| 184 |
+
page = context.new_page()
|
| 185 |
+
|
| 186 |
+
try:
|
| 187 |
+
# 设置默认超时
|
| 188 |
+
page.set_default_timeout(timeout)
|
| 189 |
+
|
| 190 |
+
print("正在导航到登录页面...")
|
| 191 |
+
page.goto("https://cwall.czoffice.top/login.php", wait_until="networkidle")
|
| 192 |
+
|
| 193 |
+
# 输入凭据
|
| 194 |
+
print("正在输入用户名和密码...")
|
| 195 |
+
page.fill("xpath=/html/body/div[2]/form/div[1]/input", username)
|
| 196 |
+
page.fill("xpath=/html/body/div[2]/form/div[2]/input", password)
|
| 197 |
+
|
| 198 |
+
# 点击登录
|
| 199 |
+
print("正在提交登录表单...")
|
| 200 |
+
page.click("xpath=/html/body/div[2]/form/input[@type='submit']")
|
| 201 |
+
|
| 202 |
+
# 等待登录完成
|
| 203 |
+
print("等待登录完成...")
|
| 204 |
+
try:
|
| 205 |
+
# 尝试检测登录成功元素
|
| 206 |
+
page.wait_for_selector("xpath=//*[contains(text(),'欢迎') or contains(text(),'Dashboard')]",
|
| 207 |
+
timeout=5000)
|
| 208 |
+
print("登录成功!")
|
| 209 |
+
except:
|
| 210 |
+
print("未检测到欢迎信息,但可能已登录成功")
|
| 211 |
+
|
| 212 |
+
if keep_open:
|
| 213 |
+
print(f"浏览器将保持打开,按Ctrl+C终止程序...")
|
| 214 |
+
while True:
|
| 215 |
+
time.sleep(1)
|
| 216 |
+
context.storage_state(path="auth.json")
|
| 217 |
+
# 读取auth.json文件
|
| 218 |
+
with open('auth.json', 'r', encoding='utf-8') as file:
|
| 219 |
+
data = json.load(file)
|
| 220 |
+
|
| 221 |
+
# 提取PHPSESSID
|
| 222 |
+
phpsessid = None
|
| 223 |
+
for cookie in data['cookies']:
|
| 224 |
+
if cookie['name'] == 'PHPSESSID':
|
| 225 |
+
phpsessid = cookie['value']
|
| 226 |
+
break
|
| 227 |
+
|
| 228 |
+
if phpsessid:
|
| 229 |
+
print("提取到的PHPSESSID是:", phpsessid)
|
| 230 |
+
else:
|
| 231 |
+
print("未找到PHPSESSID")
|
| 232 |
+
return True
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
except Exception as e:
|
| 236 |
+
print(f"自动化过程中发生错误: {str(e)}")
|
| 237 |
+
# 出错时截图保存
|
| 238 |
+
page.screenshot(path="error_screenshot.png")
|
| 239 |
+
print("已保存错误截图: error_screenshot.png")
|
| 240 |
+
return False
|
| 241 |
+
|
| 242 |
+
finally:
|
| 243 |
+
if not keep_open:
|
| 244 |
+
browser.close()
|
| 245 |
+
def get_cwall_ww_info(
|
| 246 |
+
page: str = "1"
|
| 247 |
+
):
|
| 248 |
+
"""
|
| 249 |
+
自动化解析Cwall ww内容
|
| 250 |
+
|
| 251 |
+
参数:
|
| 252 |
+
page (str): 页码
|
| 253 |
+
返回:
|
| 254 |
+
list: 包含所有ww信息的列表,每个元素是一个字典
|
| 255 |
+
"""
|
| 256 |
+
result = []
|
| 257 |
+
#请求Cwall API
|
| 258 |
+
response = requests.get("https://cwall.czoffice.top/ww/fetch_contents.php?page="+page)
|
| 259 |
+
# 将响应内容解析为 Python 字典
|
| 260 |
+
if response.status_code == 200:
|
| 261 |
+
data_dict = response.json()
|
| 262 |
+
|
| 263 |
+
# 检查请求是否成功
|
| 264 |
+
if data_dict.get('success'):
|
| 265 |
+
# 遍历 data 列表
|
| 266 |
+
for item in data_dict.get('data', []):
|
| 267 |
+
result.append({
|
| 268 |
+
"ID": item.get('id'),
|
| 269 |
+
"UserID": item.get('user_id'),
|
| 270 |
+
"用户名": item.get('username'),
|
| 271 |
+
"内容": item.get('content'),
|
| 272 |
+
"发布时间": item.get('publish_time'),
|
| 273 |
+
"点赞数": item.get('like'),
|
| 274 |
+
"图片列表": item.get('images')
|
| 275 |
+
})
|
| 276 |
+
return result
|
| 277 |
+
else:
|
| 278 |
+
print("请求失败")
|
| 279 |
+
return None
|
| 280 |
+
else:
|
| 281 |
+
print(f"请求失败,状态码: {response.status_code}")
|
| 282 |
+
return None
|
| 283 |
+
def clear_auth():
|
| 284 |
+
if os.path.exists("auth.json"):
|
| 285 |
+
os.remove("auth.json")
|
| 286 |
+
print("auth.json已删除")
|
| 287 |
+
else:
|
| 288 |
+
print("auth.json不存在")
|
| 289 |
+
def ww_like(
|
| 290 |
+
post_id: str,
|
| 291 |
+
headless: bool = False,
|
| 292 |
+
timeout: int = 30000,
|
| 293 |
+
keep_open: bool = False
|
| 294 |
+
):
|
| 295 |
+
"""
|
| 296 |
+
自动化ww点赞/取消
|
| 297 |
+
|
| 298 |
+
参数:
|
| 299 |
+
Post_id(str): ww的ID
|
| 300 |
+
headless (bool): 是否无头模式,默认显示浏览器
|
| 301 |
+
timeout (int): 超时时间(毫秒),默认30秒
|
| 302 |
+
keep_open (bool): 完成后是否保持浏��器打开
|
| 303 |
+
"""
|
| 304 |
+
with sync_playwright() as p:
|
| 305 |
+
browser = p.chromium.launch(headless=headless)
|
| 306 |
+
context = browser.new_context(storage_state="auth.json")
|
| 307 |
+
page = context.new_page()
|
| 308 |
+
try:
|
| 309 |
+
page.set_default_timeout(timeout)
|
| 310 |
+
print("正在导航ww页面...")
|
| 311 |
+
page.goto("https://cwall.czoffice.top/ww/detail.php?id="+post_id)
|
| 312 |
+
|
| 313 |
+
#点击点赞按钮
|
| 314 |
+
print("正在点赞")
|
| 315 |
+
page.click("xpath=/html/body/div/div[1]/button[1]")
|
| 316 |
+
time.sleep(1)
|
| 317 |
+
if keep_open:
|
| 318 |
+
print(f"浏览器将保持打开,按Ctrl+C终止程序...")
|
| 319 |
+
while True:
|
| 320 |
+
time.sleep(1)
|
| 321 |
+
return True
|
| 322 |
+
except Exception as e:
|
| 323 |
+
print("自动化过程出现错误")
|
| 324 |
+
page.screenshot(path="error-like.png")
|
| 325 |
+
print("已保存错误截图:error-like.png")
|
| 326 |
+
return False
|
| 327 |
+
finally:
|
| 328 |
+
if not keep_open:
|
| 329 |
+
browser.close()
|
| 330 |
+
def ww_Comment(
|
| 331 |
+
info: str,
|
| 332 |
+
post_id: str,
|
| 333 |
+
headless: bool = False,
|
| 334 |
+
timeout: int = 30000,
|
| 335 |
+
keep_open: bool = False
|
| 336 |
+
):
|
| 337 |
+
"""
|
| 338 |
+
自动化ww评论
|
| 339 |
+
参数:
|
| 340 |
+
info(str): 评论内容
|
| 341 |
+
Post_id(str): ww的ID
|
| 342 |
+
headless (bool): 是否无头模式,默认显示浏览器
|
| 343 |
+
timeout (int): 超时时间(毫秒),默认30秒
|
| 344 |
+
keep_open (bool): 完成后是否保持浏览器打开
|
| 345 |
+
"""
|
| 346 |
+
with sync_playwright() as p:
|
| 347 |
+
browser = p.chromium.launch(headless=headless)
|
| 348 |
+
context = browser.new_context(storage_state="auth.json")
|
| 349 |
+
page = context.new_page()
|
| 350 |
+
try:
|
| 351 |
+
page.set_default_timeout(timeout)
|
| 352 |
+
print("正在导航ww页面...")
|
| 353 |
+
page.goto("https://cwall.czoffice.top/ww/detail.php?id=" + post_id)
|
| 354 |
+
# 输入
|
| 355 |
+
page.fill("xpath=/html/body/div/form/textarea",info)
|
| 356 |
+
#提交
|
| 357 |
+
page.click("xpath=/html/body/div/form/button")
|
| 358 |
+
if keep_open:
|
| 359 |
+
print(f"浏览器将保持打开,按Ctrl+C终止程序...")
|
| 360 |
+
while True:
|
| 361 |
+
time.sleep(1)
|
| 362 |
+
return True
|
| 363 |
+
except Exception as e:
|
| 364 |
+
print("自动化过程出现错误")
|
| 365 |
+
page.screenshot(path="error-comment.png")
|
| 366 |
+
print("已保存错误截图:error-comment.png")
|
| 367 |
+
return False
|
| 368 |
+
finally:
|
| 369 |
+
if not keep_open:
|
| 370 |
+
browser.close()
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
# 使用示例
|
| 374 |
+
if __name__ == "__main__":
|
| 375 |
+
# # 基本用法
|
| 376 |
+
# automate_cwall_login(
|
| 377 |
+
# username="一只fish (づ ●─● )づ",
|
| 378 |
+
# password="zy842530",
|
| 379 |
+
# headless=False
|
| 380 |
+
# )
|
| 381 |
+
# cwall_ww_sub(
|
| 382 |
+
# info="Xes什么私人UI,这个C拉的比面条还长", # 必需
|
| 383 |
+
# kind="Open Cwall API", # 必需
|
| 384 |
+
# kind_enable=True,
|
| 385 |
+
# image_update_enable=True,
|
| 386 |
+
# image="C://Users/张雨.WIN-KF4D01O81O2/desktop/屏幕截图 2025-05-17 185324.png", # 可选
|
| 387 |
+
# headless=True,
|
| 388 |
+
# keep_open=False
|
| 389 |
+
# )
|
| 390 |
+
|
| 391 |
+
# 高级用法 - 保持浏览器打开
|
| 392 |
+
# automate_cwall_login(
|
| 393 |
+
# username="testuser",
|
| 394 |
+
# password="testpass",
|
| 395 |
+
# keep_open=True
|
| 396 |
+
# )
|
| 397 |
+
# get_cwall_ww_info(
|
| 398 |
+
# page= "1"
|
| 399 |
+
# )
|
| 400 |
+
# clear_auth()
|
| 401 |
+
# ww_like(
|
| 402 |
+
# post_id="3432",
|
| 403 |
+
# keep_open = True,
|
| 404 |
+
# timeout=30000,
|
| 405 |
+
# )
|
| 406 |
+
ww_Comment(
|
| 407 |
+
info="OpenCwallAPI评论测试", # 必需
|
| 408 |
+
post_id="3438", # 必需
|
| 409 |
+
headless=False,
|
| 410 |
+
keep_open=False,
|
| 411 |
+
timeout=30000,
|
| 412 |
)
|