将执行环境搬迁至docker中,其中,由于多开页面在某种程度上会导致的资源紧张,需要设置

disable-dev-shm-usage.
并且在创建容器时加大shm:
--shm-size=2g
来防止资源紧张导致的错误和退出。
This commit is contained in:
Pythagodzilla 2026-01-19 23:59:17 +08:00
parent 214a8c6c74
commit 17791aaadc
4 changed files with 21 additions and 43 deletions

2
.idea/BingPoints.iml generated
View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="BingPoints" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.12" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

2
.idea/misc.xml generated
View File

@ -3,5 +3,5 @@
<component name="Black"> <component name="Black">
<option name="sdkName" value="BingPoints" /> <option name="sdkName" value="BingPoints" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="BingPoints" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12" project-jdk-type="Python SDK" />
</project> </project>

4
run.py
View File

@ -3,12 +3,10 @@ import selenium.common
from utils import Bing from utils import Bing
User_profile_chrome = ["chromium"] User_profile_chrome = ["chromium"]
User_profile_edge = ["User Data","User DataBM"]
def main(): def main():
for path in User_profile_edge:
try: try:
driver = Bing.init_driver_edge(path) driver = Bing.init_driver()
Bing.get_points(driver=driver) Bing.get_points(driver=driver)
time.sleep(10) time.sleep(10)
Bing.get_points(driver=driver) Bing.get_points(driver=driver)

View File

@ -4,43 +4,23 @@ from selenium import webdriver
class Bing: class Bing:
@staticmethod @staticmethod
def init_driver_edge(profile_path: str): def init_driver():
"""Initialize the Selenium WebDriver with Edge edge_options. """Initialize the Selenium WebDriver with Edge edge_options.
Returns: Returns:
WebDriver: The initialized Selenium WebDriver instance. WebDriver: The initialized Selenium WebDriver instance.
""" """
from selenium.webdriver.edge.service import Service from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
service = Service(r"D:\Projects\BUCT\edgedriver\msedgedriver.exe")
edge_options = Options()
# edge_options.add_argument("--headless") # Run in headless mode
edge_options.add_argument("--no-sandbox")
edge_options.add_experimental_option("detach", False)
edge_options.add_argument(f"--user-data-dir=C:\\Users\\Plane\\AppData\\Local\\Microsoft\\Edge\\{profile_path}")
# edge_options.add_argument(f"--user-data-dir=C:\\Users\\Plane\\AppData\\Local\\Microsoft\\Edge\\User Data")
# edge_options.add_argument(r"user-data-dir=")
edge_options.add_argument("--profile-directory=Profile 1")
driver = webdriver.Edge(options=edge_options, service=service)
driver.implicitly_wait(5) # Implicit wait for elements to load
return driver
@staticmethod
def init_driver_chrome(profile_path: str):
"""Initialize the Selenium WebDriver with Chrome options.
Returns:
WebDriver: The initialized Selenium WebDriver instance.
"""
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
service = Service(executable_path="/usr/bin/chromedriver")
options = Options() options = Options()
options.add_argument("--headless") # Run in headless mode # options.add_argument("--headless") # Run in headless mode
options.add_argument("--no-sandbox") options.add_argument("--no-sandbox")
options.add_argument(f'--user-data-dir=/home/pythagodzilla/.config/{profile_path}') options.add_experimental_option("detach", True)
# options.add_argument('--user-data-dir=/home/pythagodzilla/.config/chromium') options.add_argument("--disable-dev-shm-usage")
# options.add_experimental_option("detach", True) options.add_argument(f"--user-data-dir=/tmp/chrome-profile")
driver = webdriver.Chrome(service=service, options=options) # options.add_argument(f"--user-data-dir=C:\\Users\\Plane\\AppData\\Local\\Microsoft\\Edge\\{profile_path}")
# options.add_argument(f"--user-data-dir=C:\\Users\\Plane\\AppData\\Local\\Microsoft\\Edge\\User Data")
options.add_argument("--profile-directory=Default") # Use Default profile
driver = webdriver.Remote(command_executor="http://localhost:9002/wd/hub", options=options)
driver.implicitly_wait(5) # Implicit wait for elements to load driver.implicitly_wait(5) # Implicit wait for elements to load
return driver return driver