# Selenium 慢慢爬(1):從安裝開始 windows安裝與於docker container 中安裝 收錄Selenium import 錯誤時應該做的事 ## windows+vscode+chrome 電腦規格: >* 處理器: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz 2.30 GHz >* Installed RAM 16.0 GB (15.8 GB usable) windows: >* Edition Windows 11 Pro >* Version 21H2 vscode: >* June 2022 (version 1.69) chrome: >* 版本 103.0.5060.114 (正式版本) (64 位元) ### 步驟 ### 1. 下載python https://www.python.org/downloads/ > 可直接下載最新版  > 自行選擇版本 > 點選download >  >拉到最底選擇下載的文件 > ### 2. 下載anaconda https://www.anaconda.com/download/ > 直接點選下載 >  > 安裝時添加進環境變數 >  ### 3. 下載selenium > 1. 啟動vscode > 2. 下載selenium ```bash= pip install selenium ``` > 3. 檢視是否安裝成功 ```bash= pip show selenium ```  > 4. 開啟py檔 執行import selenium ### import錯誤 >  > 若是出現已安裝卻無法import,可以檢查vscode與conda版本是否相符,若不相符需要將vscode的版本改為conda的版本 > * conda版本: ```bash= python -V ``` >  > * vscode版本: >  > * 更改vscode: > 1.Fn+F1叫出工作列 > 2.打上並點擊: python: select interpreter >  > 3.換成conda 的版本即可 ### 4. 下載webdriver 以chrome為範例 > 1.確認chrome的版本 >  >  > 2.官網選擇下載版本 https://sites.google.com/chromium.org/driver/downloads?authuser=0 > 3. 解壓縮 > 4. 檢查是否可用 ```bash= from selenium import webdriver ``` ## docker+chrome 注意!!!container 內部不會跳視窗 ### 1. 建立container ```bash= docker run -dit --name crawler_Selenium(自訂container名稱) -v 外部路徑:內部路徑 -p 80(外部port):80(內部port) --network Crawlertest(自行設定) python:3.8 bash ``` #### 之後步驟都是進入container中操作 ### 2.下載chrome ```bash= apt-get update #下載chrome wget -c https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb #啟動執行檔有機會跳錯 dpkg -i google-chrome-stable_current_amd64.deb #更新一次後再重裝 apt-get update apt-get install -f dpkg -i google-chrome-stable_current_amd64.deb #根據chrome版本下載webdriver版本 google-chrome -version ``` ### 3.下載selenium ```bash= pip install selenium ``` ### 4.下載webdriver 以chrome為範例 > 1.官網選擇下載版本 https://sites.google.com/chromium.org/driver/downloads?authuser=0 > 2.安裝並存入bin ```bash= #抓取符合版本的linuxs webdriver wget https://chromedriver.storage.googleapis.com/103.0.5060.134/chromedriver_linux64.zip unzip chromedriver_linux64.zip #存入bin中 mv chromedriver /usr/bin cd /usr/bin #讓webdriver可以被執行 chmod +x chromedriver ``` ### 5.設定chromedriver docker container內不會跳視窗且須設定好不然會跳錯 > 1. 建立一個py檔 > 2.設定 ```bash= from selenium import webdriver #設定chromedriver options= webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-gpu') options.add_argument('--disable-dev-shm-usage') #完成設定 driver= webdriver.Chrome(options=options) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up