加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Test.py 3.49 KB
一键复制 编辑 原始数据 按行查看 历史
Shmily 提交于 2023-08-29 09:06 . 优化web端获取
import json
import time
from playwright.sync_api import sync_playwright
from retrying import retry
from Exceptions.GainScorecardException import ScorecardException
def save_cookie_to_file(file_name, cookies):
with open(file_name, 'w') as file:
for cookie in cookies:
file.write(f"{json.dumps(cookie)}\n")
def read_cookie_from_file(file_name):
cookies = []
try:
for line in open(file_name, "r"):
cookies.append(json.loads(line))
except Exception as e:
return False
return True, cookies
def checkAriaAttr(page, selector, attribute):
# 使用evaluate方法获取DOM节点并判断是否具有aria-hidden属性
result = page.evaluate('(selector) => { \
const element = document.querySelector(selector); \
return element ? element.getAttribute("' + attribute + '") : null; \
}', selector)
return result
def timing(func):
"""
计算程序运行耗时
:param func: 函数,修饰器用法
:return: 内部函数
"""
def inner():
startTime = time.time()
print("开始时间:", startTime)
func()
endTime = time.time()
print("结束时间:", endTime)
timeDiff = endTime - startTime # 计算的时间差为程序的执行时间,单位为秒/s
print("时间间隔(单位/秒):", timeDiff)
return inner
# @timing
def main1():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
url = page.url
if "login.live.com" != url:
while True:
try:
page.goto("https://rewards.bing.com/", timeout=2000)
except Exception:
pass
url = page.url
if "login.live.com" in url:
page.wait_for_timeout(1000)
break
while True:
print("---------------#i0116-----------------")
info = page.wait_for_selector('#i0116', state='hidden', timeout=2000)
print(info.is_visible())
print(info.is_editable())
print(info.is_disabled())
print(info.is_enabled())
print("---------------#i0118-----------------")
info = page.wait_for_selector('#i0118', state='visible')
print(info.is_visible())
print(info.is_editable())
print(info.is_disabled())
print(info.is_enabled())
print()
print()
print()
page.wait_for_timeout(1000)
page.wait_for_timeout(1000000)
print("………………………………")
context.close()
browser.close()
def mainLogo():
import sys
from loguru import logger
logger.remove()
# 配置控制台输出
logger.add(sys.stdout, level='DEBUG')
# 配置日志输出
logger.add('./log/microsoftScore_{time: YYYY_MM_DD}.log', rotation='00:00', encoding="utf-8", level='INFO')
# 记录日志
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")
@retry(stop_max_attempt_number=2, wait_fixed=3000)
def ok():
while True:
print("t")
raise ScorecardException("t")
if __name__ == "__main__":
try:
ok()
except ScorecardException:
pass
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化