Skip to content

Commit 5da48bb

Browse files
committed
浏览器渲染支持自定义参数
1 parent 73d46fb commit 5da48bb

3 files changed

Lines changed: 29 additions & 19 deletions

File tree

feapder/setting.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@
5353
SPIDER_SLEEP_TIME = 0 # 下载时间间隔(解析完一个response后休眠时间)
5454
SPIDER_TASK_COUNT = 1 # 每个parser从内存队列中获取任务的数量
5555
SPIDER_MAX_RETRY_TIMES = 100 # 每个请求最大重试次数
56-
SPIDER_AUTO_START_REQUESTS = True # 是否主动执行添加 设置为False 需要手动调用start_monitor_task,适用于多进程情况下
57-
AUTO_STOP_WHEN_SPIDER_DONE = True # 爬虫是否自动结束
56+
SPIDER_AUTO_START_REQUESTS = (
57+
True
58+
) # 是否主动执行添加 设置为False 需要手动调用start_monitor_task,适用于多进程情况下
59+
AUTO_STOP_WHEN_SPIDER_DONE = True # 爬虫是否自动结束
5860

5961
# 浏览器渲染
6062
WEBDRIVER = dict(
@@ -68,7 +70,7 @@
6870
window_size=(1024, 800), # 窗口大小
6971
executable_path=None, # 浏览器路径,默认为默认路径
7072
render_time=0, # 渲染时长,即打开网页等待指定时间后再获取源码
71-
custom_argument=['--ignore-certificate-errors'] # 自定义浏览器渲染参数
73+
custom_argument=["--ignore-certificate-errors"], # 自定义浏览器渲染参数
7274
)
7375

7476
# 爬虫启动时,重新抓取失败的requests

feapder/templates/project_template/setting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
window_size=(1024, 800), # 窗口大小
5757
executable_path=None, # 浏览器路径,默认为默认路径
5858
render_time=0, # 渲染时长,即打开网页等待指定时间后再获取源码
59+
custom_argument=["--ignore-certificate-errors"], # 自定义浏览器渲染参数
5960
)
6061

6162
# 爬虫启动时,重新抓取失败的requests

feapder/utils/webdriver.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ class WebDriver(RemoteWebDriver):
2727
FIREFOX = "FIREFOX"
2828

2929
def __init__(
30-
self,
31-
load_images=True,
32-
user_agent=None,
33-
proxy=None,
34-
headless=False,
35-
driver_type=PHANTOMJS,
36-
timeout=16,
37-
window_size=(1024, 800),
38-
executable_path=None,
39-
custom_argument=None,
40-
**kwargs
30+
self,
31+
load_images=True,
32+
user_agent=None,
33+
proxy=None,
34+
headless=False,
35+
driver_type=PHANTOMJS,
36+
timeout=16,
37+
window_size=(1024, 800),
38+
executable_path=None,
39+
custom_argument=None,
40+
**kwargs
4141
):
4242
"""
4343
webdirver 封装,支持chrome、phantomjs 和 firefox
@@ -59,7 +59,7 @@ def __init__(
5959
self._timeout = timeout
6060
self._window_size = window_size
6161
self._executable_path = executable_path
62-
self.custom_argument = custom_argument
62+
self._custom_argument = custom_argument
6363

6464
self.proxies = {}
6565
self.user_agent = None
@@ -126,6 +126,11 @@ def firefox_driver(self):
126126
firefox_options.add_argument("--headless")
127127
firefox_options.add_argument("--disable-gpu")
128128

129+
# 添加自定义的配置参数
130+
if self._custom_argument:
131+
for arg in self._custom_argument:
132+
firefox_options.add_argument(arg)
133+
129134
if self._executable_path:
130135
driver = webdriver.Firefox(
131136
capabilities=firefox_capabilities,
@@ -178,9 +183,10 @@ def chrome_driver(self):
178183
chrome_options.add_argument(
179184
"--window-size={},{}".format(self._window_size[0], self._window_size[1])
180185
)
186+
181187
# 添加自定义的配置参数
182-
if self.custom_argument:
183-
for arg in self.custom_argument:
188+
if self._custom_argument:
189+
for arg in self._custom_argument:
184190
chrome_options.add_argument(arg)
185191

186192
if self._executable_path:
@@ -221,9 +227,10 @@ def phantomjs_driver(self):
221227
)
222228
if not self._load_images:
223229
service_args.append("--load-images=no")
230+
224231
# 添加自定义的配置参数
225-
if self.custom_argument:
226-
for arg in self.custom_argument:
232+
if self._custom_argument:
233+
for arg in self._custom_argument:
227234
service_args.append(arg)
228235

229236
if self._executable_path:

0 commit comments

Comments
 (0)