云计算百科
云计算领域专业知识百科平台

在无界面ubuntu服务器上配置chrome浏览器,结合undetected_chromedriver实现数据抓取!不使用sudo权限安装chrome浏览器!

import undetected_chromedriver as uc

driver = uc.Chrome(options=options)

问题:uc.Chrome在Linux服务器运行时出现报错:  Binary Location Must be a String – Linux

其原因是因为服务器上没有安装chrome浏览器,系统找不到可执行路径。

大多数解决办法为:如下链接

sudo pacman -S chromium  或者   sudo snap install chromium  或者sudo apt install google-chrome-stable

这些都需要root用户的sodu权限,这是由于在linux中安装和运行程序的要求。但是我们在租服务器时有时不是root用户。

"TypeError: Binary Location Must be a String" on Linux Mint with basic code · Issue #2011 · ultrafunkamsterdam/undetected-chromedriverNo Binary Location / Binary Location Must be a String – Linux · Issue #1544 · ultrafunkamsterdam/undetected-chromedriver

解决:在自己账户下安装chrome浏览器:

一.安装chrome

下载 Chrome 的便携版本(无需安装)

Google 提供了一些便携式(portable)版本,可以直接下载并运行,而无需安装到系统路径。

  • 下载 Chrome 安装包:

    打开终端并运行以下命令下载 .deb 文件:

    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

  • 解压 .deb 文件:

    Debian 文件实际上是一个归档包,可以使用 ar 工具提取内容。运行以下命令(无需 sudo):

    ar x google-chrome-stable_current_amd64.deb tar -xvf data.tar.xz

    这会在当前目录解压出 Chrome 的文件。

  • 找到 Chrome 可执行文件:

    解压后,您会在 opt/google/chrome/ 目录下找到 Chrome 可执行文件。可以通过以下命令启动:

    ./opt/google/chrome/chrome此时会报错:[1128/155453.382705:WARNING:chrome_main_linux.cc(80)] Read channel stable from ./opt/google/chrome/CHROME_VERSION_EXTRA [582953:582953:1128/155453.396982:FATAL:setuid_sandbox_host.cc(163)] The SUID sandbox helper binary was found, but is not configured correctly. Rather than run without sandboxing I'm aborting now. You need to make sure that /home/zhubin/workspace/Chromium/opt/google/chrome/chrome-sandbox is owned by root and has mode 4755. Trace/breakpoint trap (core dumped)也可能出现:error while loading shared libraries: libgbm.so.1: cannot open shared object file: No such file or directory 解决办法:sudo apt update                        sudo apt install libgbm1

  • 二、无头运行chrome

    上面是一个常见的问题,尤其是在没有 sudo 权限的情况下尝试运行 Google Chrome 或 Chromium。错误信息指出 SUID sandbox helper binary 配置不正确。由于没有 sudo 权限,无法更改文件的所有者或权限。

    解决办法:./opt/google/chrome/chrome –no-sandbox –disable-setuid-sandbox –headless –disable-gpu –remote-debugging-port=9222 出现:[1128/155748.393876:WARNING:chrome_main_linux.cc(80)] Read channel stable from ./opt/google/chrome/CHROME_VERSION_EXTRA DevTools listening on ws://127.0.0.1:9222/devtools/browser/ff9e1148-3944-4415-983d-25607d77c263

    运行成功! 这意味着您已经成功启动了 Chrome 的 无头模式(Headless mode),并且浏览器正在监听调试端口 9222,等待接受远程调试连接。这种模式非常适合自动化任务,如网页抓取、自动化测试等。

    在python中使用示例代码:

    # 启动 Undetected ChromeDriver
    options = uc.options.ChromeOptions()
    # options.add_argument("–start-maximized") # 窗口最大化
    options.binary_location = "/home/workspace/Chromium/opt/google/chrome/chrome"
    # 添加参数与无头 Chrome 交互
    options.add_argument("–no-sandbox")
    options.add_argument("–disable-setuid-sandbox")
    options.add_argument("–headless")
    options.add_argument("–disable-gpu")
    options.add_argument("–remote-debugging-port=9222")

    driver = uc.Chrome(options=options)

    # 测试打开网页
    driver.get("https://www.google.com")
    print("Page Title:", driver.title)

    cookie_list=[]
    # 将每个 Cookie 添加到浏览器
    for cookie in cookie_list:
    name, value = cookie.split("=", 1) # 按等号拆分
    driver.add_cookie({
    "name": name,
    "value": value,
    "domain": "xxx.com" # 确保域名正确
    })

    # 刷新页面以应用 Cookie
    driver.refresh()
    # 打开目标页面,验证是否已成功加载 Cookie
    driver.get(url)
    redirected_url = driver.current_url # 或通过 driver.page_source 解析 HTML
    response = requests.get(redirected_url, headers=headers)
    print("Extracted URL:", redirected_url)
    # 获取整个页面的html内容
    page_source = driver.page_source

    # 关闭浏览器
    driver.quit()

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » 在无界面ubuntu服务器上配置chrome浏览器,结合undetected_chromedriver实现数据抓取!不使用sudo权限安装chrome浏览器!
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!