ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

selenium: 安装selenium

2026/8/3 18:05:14 拓冰建站 浏览量
selenium: 安装selenium

一,官网:

地址:

https://www.selenium.dev/

代码站:

https://github.com/SeleniumHQ/selenium

二,安装:

$ pip install selenium

三,安装driver

查看chrome的版本:

$ google-chrome --version
Google Chrome 142.0.7444.59 

下载driver,地址:

https://googlechromelabs.github.io/chrome-for-testing/#canary

下载完成后解压,移动到相应的目录,我们这里用/opt/soft

$ ls /opt/soft/chromedriver-linux64/
chromedriver  LICENSE.chromedriver  THIRD_PARTY_NOTICES.chromedriver

增加可执行权限:

$ chmod +x /opt/soft/chromedriver-linux64/chromedriver

 

四,代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options# 指定驱动路径
driver_path = '/opt/soft/chromedriver-linux64/chromedriver'# 创建ChromeOptions对象
chrome_options = Options()# 添加Chrome启动参数
chrome_options.add_argument("--headless")   #这两个选项务必加上
chrome_options.add_argument('--no-sandbox')# 创建Service对象并传入ChromeOptions
service = Service(driver_path)# 创建WebDriver对象
driver = webdriver.Chrome(service=service, options=chrome_options)# 打开网页
URL = 'https://movie.douban.com/explore?support_type=movie&is_all=false&category=%E7%83%AD%E9%97%A8&type=%E5%85%A8%E9%83%A8'
driver.get(URL)# 输出网页源代码
print(driver.page_source)# 关闭浏览器
driver.quit()