__init__.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. """
  3. import platform
  4. from abc import abstractmethod
  5. import api
  6. from playwright.sync_api import Playwright, sync_playwright
  7. SPLIT_CHAR = '\\' if platform.system() == 'Windows' else '/'
  8. class BaseBrowser:
  9. def __init__(self, account: str, playwright=None):
  10. api.assert_not_none(account, "账号不能为空")
  11. self.account = account
  12. self.browser = None
  13. self.page = None
  14. self.result = None
  15. self.list_result = []
  16. self.map_result = {}
  17. self.has_more = False
  18. self.playwright = playwright
  19. def __init_browser__(self, playwright):
  20. if playwright:
  21. self.playwright = playwright
  22. else:
  23. self.playwright = sync_playwright().start()
  24. self.browser = self.playwright.chromium.launch_persistent_context(
  25. # proxy={
  26. # "server": "http://127.0.0.1:7890"
  27. # },
  28. proxy=None,
  29. user_data_dir=f'.{SPLIT_CHAR}.data{SPLIT_CHAR}{self.__get_name__()}{SPLIT_CHAR}{self.account}',
  30. headless=False,
  31. slow_mo=1000,
  32. channel="chrome",
  33. ignore_https_errors=True,
  34. user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
  35. 'Chrome/126.0.0.0 Safari/537.36',
  36. args=[
  37. '--disable-blink-features=AutomationControlled',
  38. # '--incognito',
  39. '--ignore-certificate-errors-spki-list',
  40. '--disable-web-security', # 禁用 Web 安全性,类似于 ChromeOptions 中的 --ignore-certificate-errors-spki-list
  41. '--no-sandbox', # 禁用沙盒模式
  42. '--disable-dev-shm-usage', # 禁用/dev/shm使用
  43. '--disable-features=site-per-process', # 禁用每个站点的进程,类似于 ChromeOptions 中的 --no-sandbox
  44. '--ignore-certificate-errors', # 忽略证书错误
  45. '--disable-features=AutomationControlled' # 禁用与自动化相关的特性
  46. ])
  47. self.browser.add_init_script(path="./stealth.min.js")
  48. self.page = self.browser.pages[0]
  49. @abstractmethod
  50. def __get_name__(self):
  51. pass