def run(pw: Playwright, nop: str, nom: int) -> None:
browser = pw.firefox.launch(headless=True)
context = browser.new_context(viewport={'width': 1920, 'height': 1080})
page = context.new_page()
page.goto("https://www.95598.cn/osgweb/login")
page.locator(".user").click()
page.get_by_placeholder("请输入用户名/手机号/邮箱").fill(username)
page.get_by_placeholder("请输入密码").fill(password)
page.locator(".code-mask").screenshot(path="yzm.png")
# el-dropdown-menu__item
with open("yzm.png", 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
print("图形验证码:", res)
page.get_by_role("textbox", name="请输入验证码").fill(res)
page.get_by_role("button", name="登录").click()
page.get_by_role("button", name="切换用户 ").click()
count = page.locator(".el-dropdown-menu__item").count()
print("数量 %s", count)
output = ""
if count <= 1:
number, amount = get_data(page)
output += f"{number}:{amount}"
else:
for i in range(count):
page.locator(".el-dropdown-menu__item").nth(i).click()
time.sleep(2)
page.get_by_role("button", name="切换用户 ").click()
number, amount = get_data(page)
output += f"{number}:{amount}"
if i < count - 1:
output += ";"
# 将电费保存到文件
save_amount_to_file(output)
page.close()
context.close()
browser.close()
def get_data(page):
while page.locator(".cff8").get_by_text('元').inner_text(timeout=5000)[:-1] == "--":
time.sleep(1)
amount = float(page.locator(".cff8").get_by_text('元').inner_text()[:-1])
user_no = page.get_by_text("用电户号").inner_text()
number = re.findall(r'\d+', user_no)[0]
print(f"用户户号:{number}")
print(f"今日电费:{amount}")
return number, amount
https://bbs.hassbian.com/forum.php?mod=redirect&goto=findpost&ptid=21918&pid=542309