https://cloud.tencent.com/developer/article/1543285
https://blog.csdn.net/u012206617/article/details/101623098
apt update
apt --fix-broken install
确认你要下载的chrome版本和chromedriver一致
https://www.ubuntuupdates.org/ppa/google_chrome?dist=stable
http://chromedriver.storage.googleapis.com/index.html
安装chrome
sudo apt-get install libxss1 libappindicator1 libindicator7
wget http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_100.0.4896.60-1_amd64.deb
sudo dpkg -i google-chrome*.deb
#如果上面报错,运行如下命令
sudo apt-get install -f
#验证安装完成
google-chrome --version
安装pip
sudo apt-get install python3-pip
sudo pip install selenium
安装chromedriver
wget -N http://chromedriver.storage.googleapis.com/100.0.4896.60/chromedriver_linux64.zip
apt install unzip
unzip chromedriver_linux64.zip
chmod +x chromedriver
sudo mv -f chromedriver /usr/local/share/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
安装命令行使用
sudo apt-get -y install xvfb gtk2-engines-pixbuf
sudo apt-get -y install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable
# 截图功能,可选
sudo apt-get -y install imagemagick x11-apps
Xvfb -ac :99 -screen 0 1280x1024x16 & export DISPLAY=:99
示例代码 ``` from selenium import webdriver
driver = webdriver.Chrome() driver.get(“https://www.baidu.com“) print driver.title
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)
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)
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) ```