背景
很久没搞selenium了,爬虫需要使用selenium,部署到liunx上,需要使用chrome跟chromedriver。
实践过程
备注网址:https://chromedriver.chromium.org/downloads
以117版本的chrome为例,可以找到chrome、chromedriver。
1、安装chrome
wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/117.0.5938.92/linux64/chrome-linux64.zip
unzip chrome-linux64.zip
./chrome --version
2、安装chromedriver
wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/117.0.5938.92/linux64/chromedriver-linux64.zip
unzip chromedriver-linux64.zip
3、配置环境变量
4、安装selenium
pip3 install selenium
5、demo验证
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
option = webdriver.ChromeOptions()
option.add_argument("--headless")
option.add_argument('--disable-gpu')
# option.add_argument("window-size=1024,768")
option.add_argument("--no-sandbox")
s = Service(executable_path='/opt/chromedriver/chromedriver-linux64/chromedriver')#selenium4中,把executable_path重构到了service中,如果系统变量中已有driver路径,则可以省略此行
driver = webdriver.Chrome(service=s,options=option)
driver.get("https://www.xueqiu.com/")
cookies = driver.get_cookies()
print(cookies)
以上,over!