123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- """
- """
- import platform
- from abc import abstractmethod
- import api
- from playwright.sync_api import Playwright, sync_playwright
- SPLIT_CHAR = '\\' if platform.system() == 'Windows' else '/'
- class BaseBrowser:
- def __init__(self, phone: str, playwright=None):
- api.assert_not_none(phone, "手机号不能为空")
- self.phone = phone
- self.browser = None
- self.page = None
- self.result = None
- self.list_result = []
- self.map_result = {}
- self.has_more = False
- self.playwright = playwright
- def __init_browser__(self):
- self.sure_playwright()
- self.browser = self.playwright.chromium.launch_persistent_context(
- proxy=None,
- user_data_dir=f'.{SPLIT_CHAR}.data{SPLIT_CHAR}{self.__get_name__()}{SPLIT_CHAR}{self.phone}',
- headless=False,
- slow_mo=1000,
- channel="chrome",
- ignore_https_errors=True,
- user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
- args=[
- '--disable-blink-features=AutomationControlled',
- '--incognito',
- '--ignore-certificate-errors-spki-list',
- '--disable-web-security', # 禁用 Web 安全性,类似于 ChromeOptions 中的 --ignore-certificate-errors-spki-list
- '--no-sandbox', # 禁用沙盒模式
- '--disable-dev-shm-usage', # 禁用/dev/shm使用
- '--disable-features=site-per-process', # 禁用每个站点的进程,类似于 ChromeOptions 中的 --no-sandbox
- '--ignore-certificate-errors', # 忽略证书错误
- '--disable-features=AutomationControlled' # 禁用与自动化相关的特性
- ])
- self.browser.add_init_script(path="./stealth.min.js")
- self.page = self.browser.new_page()
- def sure_playwright(self):
- if self.playwright is None:
- self.playwright = sync_playwright().start()
- def close(self):
- if self.page is not None:
- self.page.close()
- if self.browser is not None:
- self.browser.close()
- @abstractmethod
- def __get_name__(self):
- pass
|