https://cloud.tencent.com/developer/article/1543285
https://blog.csdn.net/u012206617/article/details/101623098

  1. apt update
  2. apt --fix-broken install

确认你要下载的chrome版本和chromedriver一致
https://www.ubuntuupdates.org/ppa/google_chrome?dist=stable
http://chromedriver.storage.googleapis.com/index.html

  1. 安装chrome

    1. sudo apt-get install libxss1 libappindicator1 libindicator7
    2. wget http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_100.0.4896.60-1_amd64.deb
    3. sudo dpkg -i google-chrome*.deb
    4. #如果上面报错,运行如下命令
    5. sudo apt-get install -f
    6. #验证安装完成
    7. google-chrome --version
  2. 安装pip

    1. sudo apt-get install python3-pip
    2. sudo pip install selenium
  3. 安装chromedriver

    1. wget -N http://chromedriver.storage.googleapis.com/100.0.4896.60/chromedriver_linux64.zip
    2. apt install unzip
    3. unzip chromedriver_linux64.zip
    4. chmod +x chromedriver
    5. sudo mv -f chromedriver /usr/local/share/chromedriver
    6. sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
    7. sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
  4. 安装命令行使用

    1. sudo apt-get -y install xvfb gtk2-engines-pixbuf
    2. sudo apt-get -y install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable
    3. # 截图功能,可选
    4. sudo apt-get -y install imagemagick x11-apps
    5. Xvfb -ac :99 -screen 0 1280x1024x16 & export DISPLAY=:99
  5. 示例代码 ``` from selenium import webdriver

driver = webdriver.Chrome() driver.get(“https://www.baidu.com“) print driver.title

  1. 6. 错误unknown error: DevToolsActivePort file doesn't exist

from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument(‘—no-sandbox’) chrome_options.add_argument(‘—disable-dev-shm-usage’) chrome_options.add_argument(‘—headless’) browser = webdriver.Chrome(chrome_options=chrome_options)

  1. 7. 示例代码

from selenium import webdriver from selenium.webdriver.chrome.options import Options

‘’’ “–no-sandbox”参数是让Chrome在root权限下跑 “–headless”参数是不用打开图形界面 ‘’’ chrome_options = Options() chrome_options.add_argument(‘—no-sandbox’) chrome_options.add_argument(‘—disable-dev-shm-usage’) chrome_options.add_argument(‘—headless’) browser = webdriver.Chrome(options=chrome_options)

driver = webdriver.Chrome()

browser.get(“https://www.baidu.com“) print(browser.title)

  1. 8. 绕过 访问拒绝的代码

from selenium import webdriver from selenium.webdriver.chrome.options import Options

tokopedia = “https://tokopedia.com/

options = Options() options.add_argument(“—headless”) options.add_argument(‘—disable-gpu’) options.add_argument(‘—no-sandbox’) options.add_experimental_option(“excludeSwitches”, [“enable-automation”]) options.add_experimental_option(‘useAutomationExtension’, False)

user_agent = ‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36’ options.add_argument(‘user-agent={0}’.format(user_agent))

driver = webdriver.Chrome(options=options) driver.get(tokopedia)

print(driver.page_source) ```