当前位置: 首页> 财经> 访谈 > 创建公司网站的方案有_舆情报告范文_营销软文300字范文_网络宣传推广

创建公司网站的方案有_舆情报告范文_营销软文300字范文_网络宣传推广

时间:2025/7/17 14:24:58来源:https://blog.csdn.net/sinat_41883985/article/details/142488110 浏览次数:0次
创建公司网站的方案有_舆情报告范文_营销软文300字范文_网络宣传推广

在 CentOS 7 上安装 Selenium 通常涉及几个步骤,包括安装 Python、安装 Selenium 库、安装 WebDriver 以及配置环境。以下是详细的步骤:

1. 安装 Python 和 pip

如果你的系统中还没有安装 Python 和 pip,可以使用以下命令进行安装:

sudo yum update -y
sudo yum install -y python3
sudo yum install -y python3-pip

验证安装:

python3 --version
pip3 --version

2. 安装 Selenium 库

使用 pip 安装 Selenium:

pip3 install selenium

3. 安装 WebDriver

Selenium 需要 WebDriver 来与浏览器进行交互。根据你使用的浏览器,安装相应的 WebDriver。

安装 ChromeDriver
  1. 安装 Google Chrome

    sudo yum install -y wget
    # wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
    # sudo yum localinstall -y google-chrome-stable_current_x86_64.rpm
    # 安装低版本
    wget http://dist.control.lth.se/public/CentOS-7/x86_64/google.x86_64/google-chrome-stable-102.0.5005.115-1.x86_64.rpm
    sudo yum localinstall -y google-chrome-stable-102.0.5005.115-1.x86_64.rpm
    
  2. 使用 webdriver_manager 来安装 ChromeDriver

# install_chromedirver.py
import os
import subprocess
import shutil
import platform  # 导入 platform 模块
from webdriver_manager.chrome import ChromeDriverManager# 安装并获取最新的 ChromeDriver 版本
driver_path = ChromeDriverManager().install()
print(f"ChromeDriver downloaded to: {driver_path}")def move_chromedriver(driver_path):system = platform.system()if system in ["Linux", "Darwin"]:destination_path = "/usr/local/bin/chromedriver"elif system == "Windows":destination_path = os.path.join(os.environ['WINDIR'], 'system32', 'chromedriver.exe')else:raise Exception(f"Unsupported OS: {system}")# 确保目标目录存在os.makedirs(os.path.dirname(destination_path), exist_ok=True)# 移动文件并设置权限shutil.move(driver_path, destination_path)os.chmod(destination_path, 0o755)return destination_pathdef get_chromedriver_version(driver_path):try:# 调用命令行工具 chromedriver --versionoutput = subprocess.check_output([driver_path, '--version'])# 解码输出并获取版本号version = output.decode('utf-8').strip()return versionexcept Exception as e:print(f"Error occurred while getting ChromeDriver version: {e}")return Nonedef main():print("Moving ChromeDriver to /usr/local/bin or system32...")new_driver_path = move_chromedriver(driver_path)print(f"ChromeDriver moved to: {new_driver_path}")print("Checking ChromeDriver version...")chromedriver_version = get_chromedriver_version(new_driver_path)if chromedriver_version:print(f"ChromeDriver version: {chromedriver_version}")else:print("Failed to get ChromeDriver version.")if __name__ == "__main__":main()

3. 执行命令安装

sudo python install_chromedirver.py

4. 测试 Selenium 安装(无头模式:在某些环境中(例如:无显示器的服务器),需要以无头模式启动 Chrome。)

创建一个 Python 脚本来测试 Selenium 安装是否成功:

# test_selenium.py
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManagerchrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)# 测试代码
driver.get("http://www.baidu.com")
print(driver.title)
driver.quit()

运行脚本:

python3 test_selenium.py

如果打开了浏览器并打印了页面标题,说明 Selenium 安装成功。

总结

通过以上步骤,应该能够在 CentOS 7 上成功安装并配置 Selenium。这个过程包括安装 Python 和 pip、安装 Selenium 库、安装相应的 WebDriver(如 ChromeDriver 或 GeckoDriver),以及测试配置。

关键字:创建公司网站的方案有_舆情报告范文_营销软文300字范文_网络宣传推广

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: