调整linux下执行
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user