基本环境:centos7,python3.x
1.安装selenium
pip3 install selenium
2.安装chrome-browser
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm --no-check-certificate
yum install ./google-chrome-stable_current_x86_64.rpm
3.下载chromedriver(注意要和chrome-browser版本对应)
由于下载的chrome-browser是70版本的,所以chromedriver 选择了2.43版本
wget http://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip
解压此文件,并将文件移动到/usr/bin目录下
unzip chromedriver_linx64.zip
mv chromedriver /usr/bin/
4.测试selenium是否可用,请执行以下python脚本,如返回html内容,则说明安装成功
from selenium import webdriver
url='http://bing.com'
option = webdriver.ChromeOptions()
option.add_argument('--no-sandbox')
option.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=option)
driver.get(url)
print(driver.page_source)
注意需要加上,禁止在沙箱中运行
option.add_argument('--no-sandbox')
option.add_argument('--headless')
补充:
安装firefox支持
yum install firefox
yum install Xvfb
yum install libXfont
pip3 install pyvirtualdisplay
测试脚本
#!/usr/bin/python3
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800,600))
display.start()
driver = webdriver.Firefox()
driver.get('https://www.baidu.com')
print(driver.title)
driver.quit()
display.stop()
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。