在linux服務器上安裝chrome :
ubuntu:
下載頁面https://www.chrome64bit.com/index.php/google-chrome-64-bit-for-linux
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 下載
sudo apt-get install libxss1 libappindicator1 libindicator7 安裝依賴
sudo dpkg -i google-chrome*.deb 這條語句可能報錯,下條命令將修復它
sudo apt-get install -f
google-chrome 查看版本號
centos:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm 下載
yum localinstall google-chrome-stable_current_x86_64.rpm 在當前目錄下使用本地安裝,yum會自動分析依賴,完成安裝
headless chrome的基本操作:https://developers.google.com/web/updates/2017/04/headless-chrome
啟用無頭chrome訪問網頁並打印頁面 --dump-dom用於打印頁面的document.body.innerHTML到標准輸出:
chrome --headless --disable-gpu --dump-dom https://www.baidu.com/
截取網頁截圖, --screenshot 將會截取網頁截圖並保存為當前目錄的screenshot.png文件:
chrome --headless --disable-gpu --screenshot --window-size=1280,1696 https://www.baidu.com
使用代理 --proxy-server=http://ip:port 訪問頁面,並保存截圖
chrome --headless --disable-gpu --screenshot --proxy-server=socks5://127.0.0.1:1080 https://www.google.com
python通過selenium使用chrome headless :
下載chromedriver, 頁面地址 http://npm.taobao.org/mirrors/chromedriver/71.0.3578.80/
由於安裝的chrome是 71.0.3578.98版本的,選了個最接近的chromedriver。
wget http://npm.taobao.org/mirrors/chromedriver/71.0.3578.80/chromedriver_linux64.zip
在window上解壓后將chromedriver文件上傳到/usr/bin/目錄下
pip install selenium
代碼示例:
from selenium import webdriver
PROXY='http://ip:port' #或 PROXY=’socks5://ip:port'
my_options=webdriver.ChromeOptions()
my_options.add_argument('--headless') #添加無頭參數
my_options.add_argument('--disable-gpu') #添加無頭參數
my_options.add_argument('--no-sandbox') #添加無頭參數
#my_options.add_argument('lang=zh_CN.UTF-8')
my_options.add_argument('--allow-running-insecure-content') #設置proxy時添加的額外參數,允許不安全的證書
my_options.add_argument('--ignore-certificate-errors') #設置proxy時添加的額外參數,允許不安全的證書
my_options.add_argument("user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'") #設置用戶代理
caps = DesiredCapabilities.CHROME.copy()
caps['proxy']={'httpProxy':PROXY, #添加代理IP proxy
'ftpProxy':PROXY,
'sslProxy':PROXY,
'noProxy':None,
'proxyType':'MANUAL',
'class':'org.openqa.selenium.Proxy',
'autodetect':False}
caps['acceptSslCerts'] = True #設置允許不安全的證書
caps['acceptInsecureCerts'] = True
bs=webdriver.Chrome(chrome_options=my_options,executable_path='/usr/bin/webdriver',desired_capabilities=caps)
bs.get('https://www.baidu.com')
chrome_options : ChromeOptions()對象
executable_path : chromedriver的絕對路徑
desired_capabilities :一般為對應瀏覽器的DesiredCapabilities的copy(),避免更改默認設置
本地shadow(**隔離**)socks安裝 pip install shadow(**隔離**)socks
配置shadow(**隔離**)socks連接參數 ,創建文件shadow(**隔離**)socks.json
{
"server":"153.234.1.5",
"server_port":1108,
"local_address": "0.0.0.0",
"local_port":1018
"password":"sdsewedxxf#",
"timeout":300,
"method":"chacha20",
"fast_open": false
}
啟動shadow(**隔離**)socks客戶端 sslocal -c shadow(**隔離**)socks.json -d start
-d 表示后台啟動, -c表示使用文件shadow(**隔離**)socks.json中的配置