调整linux下执行

This commit is contained in:
liuwei
2025-04-30 16:47:23 +08:00
parent b62bc2d191
commit 8ec89831a7

View File

@@ -45,10 +45,29 @@ def fetch_and_create_pdf(url):
"""根据给定URL抓取页面并生成PDF"""
# 配置Selenium
options = Options()
options.headless = True
options.add_argument('--headless') # 使用新的headless模式
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage') # 添加Linux特定配置
# 根据操作系统选择不同的ChromeDriver路径处理方式
if os.name == 'nt': # Windows
chrome_driver_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"utils", "chromedriver", "chromedriver.exe"
)
else: # Linux
chrome_driver_path = 'chromedriver' # 使用系统PATH中的chromedriver
try:
if os.name == 'nt' and not os.path.exists(chrome_driver_path):
chrome_driver_path = ChromeDriverManager().install()
driver = webdriver.Chrome(service=Service(chrome_driver_path), options=options)
except Exception as e:
print(f"初始化ChromeDriver失败: {e}")
chrome_driver_path = ChromeDriverManager().install()
driver = webdriver.Chrome(service=Service(chrome_driver_path), options=options)
# 使用本地固定的ChromeDriver路径避免每次自动更新
chrome_driver_path = os.path.join(