简体   繁体   English

如何减少 selenium python 中的 chromedriver cpu 使用率?

[英]How to reduce chromedriver cpu usage in selenium python?

i'm trying to automate login and scraping data using selenium in python im using chromedriver as a driver.我正在尝试使用 python 中的 selenium 自动登录和抓取数据,我使用 chromedriver 作为驱动程序。 but i want to do that with 5 account, im still figuring out what is the best method to do.但我想用 5 个帐户来做,我还在想什么是最好的方法。

for now, i create the code in python file then i create batch file to run the python. so i can open multiple batch file with multiple account.现在,我在 python 文件中创建代码,然后我创建批处理文件来运行 python。这样我就可以用多个帐户打开多个批处理文件。

but the problem is cpu usage is too high so im just able to do with 3 account.但问题是 CPU 使用率太高,所以我只能使用 3 个帐户。

Troubleshooting i do so far is changing options to make chrome headless using this code到目前为止我所做的故障排除是更改选项以使用此代码使 chrome 无头

    self.options = webdriver.ChromeOptions()
    self.options.headless = True
    self.options.add_argument(f'user-agent={user_agent}')
    self.options.add_argument("--window-size=1920,1080")
    self.options.add_argument('--ignore-certificate-errors')
    self.options.add_argument('--allow-running-insecure-content')
    self.options.add_argument("--disable-extensions")
    self.options.add_argument("--proxy-server='direct://'")
    self.options.add_argument("--proxy-bypass-list=*")
    self.options.add_argument("--start-maximized")
    self.options.add_argument('--disable-gpu')
    self.options.add_argument('--disable-dev-shm-usage')
    self.options.add_argument("--FontRenderHinting[none]")
    self.options.add_argument('--no-sandbox')
    self.options.add_argument('log-level=3')
    self.driver = webdriver.Chrome(executable_path="chromedriver.exe", options=self.options)

but still cant achieve my target(5 account).但仍然无法实现我的目标(5 个帐户)。 is there anything i can do to achieve my target?我能做些什么来实现我的目标吗?

Thanks.谢谢。

I have noticed (by watching htop processes while tests are running), the crash reporting of chrome takes up a huge amount of CPU and memory我注意到(通过在测试运行时观察 htop 进程),chrome 的崩溃报告占用了大量 CPU 和 memory

Since you are using Chrome Headless, I've found adding this reduces the CPU usage by about 20% for me:由于您使用的是 Chrome Headless,我发现添加它可以为我减少大约 20% 的 CPU 使用率:

--disable-crash-reporter --disable-crash-reporter

This will only disable when you are running in headless This might speed things up for you!!!这只会在您无头运行时禁用这可能会加快您的速度!!!

My settings are currently as follows and I reduce the CPU (but only a marginal time saving) by about 20%:我目前的设置如下,我减少了大约 20% 的 CPU(但只是节省了边际时间):

    self.options.add_argument("--no-sandbox");
    self.options.add_argument("--disable-dev-shm-usage");
    self.options.add_argument("--disable-renderer-backgrounding");
    self.options.add_argument("--disable-background-timer-throttling");
    self.options.add_argument("--disable-backgrounding-occluded-windows");
    self.options.add_argument("--disable-client-side-phishing-detection");
    self.options.add_argument("--disable-crash-reporter");
    self.options.add_argument("--disable-oopr-debug-crash-dump");
    self.options.add_argument("--no-crash-upload");
    self.options.add_argument("--disable-gpu");
    self.options.add_argument("--disable-extensions");
    self.options.add_argument("--disable-low-res-tiling");
    self.options.add_argument("--log-level=3");
    self.options.add_argument("--silent");

I found this to be a pretty good list (full list I think) of command line switches with explanations: https://peter.sh/experiments/chromium-command-line-switches/我发现这是一个很好的命令行开关列表(我认为是完整列表),并附有解释: https://peter.sh/experiments/chromium-command-line-switches/

Some additional things you can turn off are also mentioned here: https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md此处还提到了一些您可以关闭的其他功能: https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md

I hope this helps someone我希望这可以帮助别人

self.options.add_argument('--headless')

this will make the driver run way faster then before这将使司机跑得比以前更快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM