使用undetected_chromedriver进行网页抓取,ubuntu/windows 11兼容性问题

cgyqldqp  于 2023-10-14  发布在  Go
关注(0)|答案(1)|浏览(351)

我用python编写了discord bot,它基本上是webscraping chat gpt 3.5,并将答案发送到我的discord服务器。它可以在Windows 11上运行,但无论我做什么,我都不能在我的Ubuntu服务器上运行它。下面是输出错误的代码(第25行的所有内容):

from bs4 import BeautifulSoup
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By # target

import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!", intents=discord.Intents.all())


import time


# files
#import login


options = uc.ChromeOptions() 
options.headless = False

chrome = uc.Chrome(options=options)

就像我说的,它在windows上工作,但是当我在我的ubuntu服务器上运行它时(使用这个命令:xvfb-run --server-args="-screen 0 1920x1080x24" python3 AiSupport.py,我需要xvfb,所以我可以在headful模式下运行它,未检测到的Chrome驱动程序在headless模式下不工作)它给出了这个错误:

ubuntu@instance-20230620-2312:~/bot$ xvfb-run --server-args="-screen 0 1920x1080                                                                                                                                                                                                x24" python3 AiSupport.py
Traceback (most recent call last):
  File "/home/ubuntu/bot/AiSupport.py", line 25, in <module>
    chrome = uc.Chrome(options=options)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/undetected_chromedriver                                                                                                                                                                                                /__init__.py", line 466, in __init__
    super(Chrome, self).__init__(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/chro                                                                                                                                                                                                me/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/chro                                                                                                                                                                                                mium/webdriver.py", line 53, in __init__
    self.service.start()
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/comm                                                                                                                                                                                                on/service.py", line 98, in start
    self._start_process(self._path)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/comm                                                                                                                                                                                                on/service.py", line 204, in _start_process
    self.process = subprocess.Popen(
  File "/usr/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.10/subprocess.py", line 1863, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/home/ubuntu/.local/share/undetected_chro                                                                                                                                                                                                medriver/undetected_chromedriver'

我试着在我的ubuntu服务器上更新python和模块,没有工作。

vcirk6k6

vcirk6k61#

您可以使用SeleniumBase的UC模式作为替代方案,它对undetected-chromedriver进行了略有修改。
pip install seleniumbase。然后使用python运行以下脚本:

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.driver.get("https://nowsecure.nl/#relax")
    sb.sleep(3)

它会根据环境自动配置设置。例如,它知道何时在无头Linux环境中使用Xvfb。

相关问题