import sys
import time

from DrissionPage import Chromium, ChromiumOptions
from loguru import logger

chrome_opts = ChromiumOptions()
chrome_opts.mute(True)  # 静音
chrome_opts.no_imgs(False)
chrome_opts.set_argument("--disable-gpu")
chrome_opts.set_argument('--ignore-certificate-errors')
chrome_opts.set_argument("--proxy-server=http://127.0.0.1:7890")
# chrome_opts.incognito(True)
chrome_opts.set_browser_path(r"C:\Program Files\Google\Chrome\Application\chrome.exe")
browser = Chromium(addr_or_opts=chrome_opts)

tab = browser.new_tab()

domain = "lightless.me"
end_time = int(time.time())
start_time = end_time - 3600 * 24 * 30
# tab.get(f"https://www.baidu.com/s?wd=site%3A{domain}&&gpc=stf%3D{start_time}%2C{end_time}%7Cstftype%3D1")
tab.get(f"https://www.baidu.com/s?wd=site%3A{domain}")

tab.wait.eles_loaded("@id=container")

# print(tab.html)
logger.debug("1")
if "抱歉，未找到相关结果。" in tab.html:
    print("no result")
    browser.quit()
    sys.exit()


tab.wait.eles_loaded("@id=content_left")
logger.debug("2")
el = tab.ele("@id=content_left")

results = el.eles("@class:result")

# tab.wait.eles_loaded("@class:result c-container xpath-log new-pmd")
# logger.debug("3")
# results = tab.eles("@class:result c-container xpath-log new-pmd")
# logger.debug("4")
for result in results:
    logger.debug(result)
    logger.debug("找到了一个URL：{}", result.attr("mu"))


browser.quit()