linux下安装 Chrome 和 chromedriver 以及 selenium webdriver 使用

1 安装 Chrome

sh 复制代码
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

2 下载 chromedriver

sh 复制代码
# 进入下载目录
cd soft/crawler_tools

# 查看chrome 版本号
google-chrome --version

# 在chromedriver下载地址中找到对应版本,下载对应版本chromedriver
wget url
# 如:
wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/118.0.5993.70/linux64/chromedriver-linux64.zip

# 解压
unzip chromedriver-linux64.zip

# 赋权
cd chromedriver-linux64
chmod +x chromedriver

chromedriver 下载地址:

https://googlechromelabs.github.io/chrome-for-testing/ (推荐,包含最新稳定版)

https://chromedriver.storage.googleapis.com/index.html?

http://npm.taobao.org/mirrors/chromedriver/

https://registry.npmmirror.com/-/binary/chromedriver/

查看版本:

chromedriver对应下载地址

3 使用 selenium webdriver

现在就可以使用 selenium 的 webdriver 爬取内容了

python 复制代码
from selenium import webdriver

class SeleniumWebdriver:
    def __init__(self):
        self.chrome_options = webdriver.ChromeOptions()
        self.chrome_options.add_argument('--headless')
        self.chrome_options.add_argument(
            'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36')
        # chromedriver 地址
        self._CHROME_DRIVER_LINUX = '/soft/crawler_tools/chromedriver'
    # 浏览器模拟的方式获取含有专辑 mid 信息的页面
    def get_pagesource_by_chrome(self, url):
        browser = webdriver.Chrome(executable_path=self._CHROME_DRIVER_LINUX, options=self.chrome_options)
        browser.get(url)
        time.sleep(2)
        res = browser.page_source
        browser.close()
        return res
if __name__ == "__main__":
    sw= SeleniumWebdriver()
    url = "www.baidu.com"
    content = sw.get_pagesource_by_chrome(url)
    print(content)

参考:
傻瓜式linux下安装Chrome和chromedriver
chromedriver高于114版本的版本如115、116、117、118等,如何下载对应版本

相关推荐
杨云龙UP20 分钟前
ODA服务器RAC节点2/u01分区在线扩容操作记录及后续处理流程(Linux LVM + ext4 文件系统在线扩容操作手册)_20260307
linux·运维·服务器·数据库·ubuntu·centos
jyfool8 小时前
Ubuntu 远程桌面配置踩坑实录:从 TightVNC 到 x11vnc 的折腾之旅
linux·运维·ubuntu
安当加密8 小时前
基于 RADIUS 的 Linux 服务器双因子认证:从 FreeRADIUS 到轻量级 ASP 方案的演进
linux·运维·服务器
66清小风9 小时前
服务器安装操作系统报X or window manager startup failed, falling back to mode
linux·kylin
double2li9 小时前
linux/unix 段错误捕获【续】
linux·运维·服务器·网络·unix
allway29 小时前
Linux / Unix last Command Examples
linux·运维·unix
嘿嘿嘿x310 小时前
MobaXterm 成功连接 Ubuntu 虚拟机
linux·运维·ubuntu
万能菜道人10 小时前
多次连接共享内存大小不一致在win和linux的区别
linux·运维·服务器
嵌入式×边缘AI:打怪升级日志10 小时前
2.3.1 Linux 命令行介绍:Shell、PATH与第一个Hello程序
linux·运维·服务器
猪猪侠|ZZXia10 小时前
# Openssl关键知识
linux·网络