webdirver.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2022/9/7 4:27 PM
  4. ---------
  5. @summary:
  6. ---------
  7. @author: Boris
  8. @email: boris_liu@foxmail.com
  9. """
  10. import abc
  11. from feapder import setting
  12. class InterceptRequest:
  13. def __init__(self, url, data, headers):
  14. self.url = url
  15. self.data = data
  16. self.headers = headers
  17. class InterceptResponse:
  18. def __init__(self, request: InterceptRequest, url, headers, content, status_code):
  19. self.request = request
  20. self.url = url
  21. self.headers = headers
  22. self.content = content
  23. self.status_code = status_code
  24. class WebDriver:
  25. def __init__(
  26. self,
  27. load_images=True,
  28. user_agent=None,
  29. proxy=None,
  30. headless=False,
  31. driver_type=None,
  32. timeout=16,
  33. window_size=(1024, 800),
  34. executable_path=None,
  35. custom_argument=None,
  36. download_path=None,
  37. auto_install_driver=True,
  38. use_stealth_js=True,
  39. **kwargs,
  40. ):
  41. """
  42. webdirver 封装,支持chrome、phantomjs 和 firefox
  43. Args:
  44. load_images: 是否加载图片
  45. user_agent: 字符串 或 无参函数,返回值为user_agent
  46. proxy: xxx.xxx.xxx.xxx:xxxx 或 无参函数,返回值为代理地址
  47. headless: 是否启用无头模式
  48. driver_type: CHROME,EDGE 或 PHANTOMJS,FIREFOX
  49. timeout: 请求超时时间
  50. window_size: # 窗口大小
  51. executable_path: 浏览器路径,默认为默认路径
  52. custom_argument: 自定义参数 用于webdriver.Chrome(options=chrome_options, **kwargs)
  53. download_path: 文件下载保存路径;如果指定,不再出现“保留”“放弃”提示,仅对Chrome有效
  54. auto_install_driver: 自动下载浏览器驱动 支持chrome 和 firefox
  55. use_stealth_js: 使用stealth.min.js隐藏浏览器特征
  56. **kwargs:
  57. """
  58. self._load_images = load_images
  59. self._user_agent = user_agent or setting.DEFAULT_USERAGENT
  60. self._proxy = proxy
  61. self._headless = headless
  62. self._timeout = timeout
  63. self._window_size = window_size
  64. self._executable_path = executable_path
  65. self._custom_argument = custom_argument
  66. self._download_path = download_path
  67. self._auto_install_driver = auto_install_driver
  68. self._use_stealth_js = use_stealth_js
  69. self._driver_type = driver_type
  70. self._kwargs = kwargs
  71. @abc.abstractmethod
  72. def quit(self):
  73. pass