一,chrome浏览器设置

from selenium import webdriver
# 浏览器选项
chrome_options = webdriver.ChromeOptions()
# 使用headless无界面浏览器模式
chrome_options.add_argument('--headless')
# 谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--disable-gpu')
# 设置默认编码为utf-8
chrome_options.add_argument('lang=zh_CN.UTF-8')
# 隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('--hide-scrollbars')
# 禁止加载图片
chrome_options.add_argument('blink-settings=imagesEnabled=false')
# 指定浏览器分辨率
chrome_options.add_argument('window-size=1440x900')
# 设置默认请求头
chrome_options.add_argument('user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X)AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"')
# 设置代理
desired_capabilities = chrome_options.to_capabilities()
desired_capabilities['proxy'] = {
    "httpProxy":PROXY,
    "ftpProxy":PROXY,
    "sslProxy":PROXY,
    "noProxy":None,
    "proxyType":"MANUAL",
    "class":"org.openqa.selenium.Proxy",
    "autodetect":False
}
# 启动浏览器,获取网页源代码
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(url)
# 显示页面源码
html = driver.page_source
# 关闭当前页面
driver.close()
# 退出浏览器
driver.quit()

  

二,Firefox浏览器设置

from selenium import webdriver
# 浏览器选项
firefox_options = webdriver.FirefoxOptions()
# 使用headless无界面浏览器模式
firefox_options.add_argument('--headless')
# 谷歌文档提到需要加上这个属性来规避bug
firefox_options.add_argument('--disable-gpu')
# 设置默认编码为utf-8
firefox_options.add_argument('lang=zh_CN.UTF-8')
# 隐藏滚动条, 应对一些特殊页面
firefox_options.add_argument('--hide-scrollbars')
# 禁止加载图片
firefox_options.add_argument('blink-settings=imagesEnabled=false')
# 指定浏览器分辨率
firefox_options.add_argument('window-size=1440x900')
# driver.maximize_window()
# 设置默认请求头
firefox_options.add_argument('user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X)AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"')
# 设置代理
desired_capabilities = firefox_options.to_capabilities()
desired_capabilities['proxy'] = {
    "httpProxy":PROXY,
    "ftpProxy":PROXY,
    "sslProxy":PROXY,
    "noProxy":None,
    "proxyType":"MANUAL",
    "class":"org.openqa.selenium.Proxy",
    "autodetect":False
}
# 启动浏览器,获取网页源代码
driver = webdriver.Firefox(firefox_options=firefox_options)
driver.get(url)
# 显示页面源码
html = driver.page_source
# 关闭当前页面
driver.close()
# 退出浏览器
driver.quit()

 三,部署

1.centos7无桌面环境部署

环境要求:

CentOS 7
Firefox 56.0+
Selenium 3.5+
geckodriver 0.19+

Xvfb(X virtual framebuffer)是一个虚拟显示服务器,不需要显示设备也能模拟运行图形界面

安装 Xvfb:

yum install xorg-x11-server-Xvfb bzip gtk3

安装火狐浏览器

cd /usr/local
wget https://ftp.mozilla.org/pub/firefox/releases/56.0.2/linux-x86_64/en-US/firefox-56.0.2.tar.bz2
tar -jxvf firefox-56.0.2.tar.bz2
ln -s /usr/local/firefox/firefox /usr/bin/firefox
rm -rf firefox-56.0.2.tar.bz2

 安装selenium

pip3 install selenium

 安装Firefoxdriver

wget https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux64.tar.gz
tar -zxvf geckodriver-*.tar.gz
ln -s /root/geckodriver /usr/bin/geckodriver

# 最新版本下载
https://github.com/mozilla/geckodriver/releases

 运行测试:

1.启动虚拟桌面坏境,保持后台运行。

Xvfb :1 -screen 0 1024x768x24 &

:1是服务启动的端口号,可以任意设置,与下一步保持一致就行

2.配置环境变量

export DISPLAY=:1

端口号和上面一致,冒号不能漏

3.启动程序测试

from selenium import webdriver
b = webdriver.Firefox(executable_path='/root/geckodriver')
b.get('http://www.baidu.com')
print(b.page_source)
b.quit()

  

  

1.3最新chromedriver安装

phantomjs逐步淡出我们的实现,已经不再被支持,chrome集高并发的优点,且目前也已支持无头浏览器。

安装chrome

curl https://intoli.com/install-google-chrome.sh | bash

测试

google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot     https://www.suning.com/ # 在当前路径下生成一张截图。

下载最新版本的chromedriver,

https://sites.google.com/a/chromium.org/chromedriver/downloads
# 解压
$ unzip chromedriver_linux64.zip

# 测试
$ ./chromedriver

# 加入环境变量

  

引入虚拟界面(实际没有这样做)

from pyvirtualdisplay import Display
display = Display(visible=0,size=(800,600))
display.start()
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')

print(driver.page_source)

实际环境中采用的写法,并成功了.

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
# 使用代理ip
chrome_options.add_argument("--proxy-server=http://202.20.16.82:10152")
# 使用headless无界面浏览器模式
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
# 谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--disable-gpu')
# 禁止加载图片
chrome_options.add_argument('blink-settings=imagesEnabled=false')
# 设置默认请求头
chrome_options.add_argument("user-agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'")
wd = webdriver.Chrome(chrome_options=chrome_options,executable_path='/home/chrome/chromedriver')

wd.get("https://www.163.com")

content = wd.page_source.encode('utf-8')
print(content)

wd.quit()

  

selenium 浏览器常用设置和部署的更多相关文章

  1. linux云服务器常用设置

    前面的话 由于在云服务器上配置自己的网站,将Linux里的常用设置记录如下 更改shell 默认地, ubuntu系统默认的shell是dash,但更常用的shell是bash 通过下面命令可以将da ...

  2. python3 selenium模块Chrome设置代理ip的实现

    python3 selenium模块Chrome设置代理ip的实现 selenium模块Chrome设置代理ip的实现代码: from selenium import webdriver chrome ...

  3. Selenium浏览器自动化测试使用(2)

    Selenium - 环境安装设置 为了开发Selenium RC或webdriver脚本,用户必须确保他们有初始配置完成.有很多关联建立环境的步骤.这里将通过详细的讲解. 下载并安装Java 下载并 ...

  4. 《Pro Express.js》学习笔记——Express框架常用设置项

    Express 设置 系统设置 1.       无须再定义,大部分有默认值,可不设置 2.       常用设置 env view cache view engine views trust pro ...

  5. Eclipse下Tomcat常用设置

    Eclipse下Tomcat常用设置 1,Eclipse建立Tomcat服务 1.1 新建Server 首先这里是指,jee版的Eclipse.Eclipse是没有像MyEclipse那样集成Tomc ...

  6. Python入门之PyCharm的快捷键与常用设置和扩展(Mac系统)

    1. 快捷键 2 . PyCharm的常用设置和扩展 ------------------------------------------------------------------------- ...

  7. redis常用服务安装部署

    常用服务安装部署   学了前面的Linux基础,想必童鞋们是不是更感兴趣了?接下来就学习常用服务部署吧! 安装环境: centos7 + vmware + xshell 即将登场的是: mysql(m ...

  8. Eclipse 使用前常用设置

    1.常用设置的位置 Eclipse中一般的设置都是在这个位置进行设置的: 2.设置字体类型和大小 一般可以设置成这样代码比较清晰:Consolas + 常规 + 小四 3.设置各种编码 设置工作空间的 ...

  9. HTML head标签内部常用设置

    HTML head标签内部常用设置 在网页html文件中,head标签里面通常放置的代码是用来对网页进行相关设置的内容.下面就是对这些内容的介绍. meta标签的设置 在网页中,meta标签最常用的设 ...

随机推荐

  1. Apached+resin服务搭建

    一.前言 Resin是CAUCHO公司的产品,是一个非常流行的支持servlets 和jsp的引擎,速度非常快.对servlet和JSP提供了良好的支持,性能也比较优良,resin自身采用JAVA语言 ...

  2. Uncaught (in promise) DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL

    解决方案:url前面一定要加http://

  3. 动态参数(*args,**kwargs),命名空间和作用域,global和nonlocal,函数的嵌套

    1. 动态参数 位置参数的动态参数: *args 关键字参数的动态参数 : **kwargs 顺序: 位置,*args,默认值,**kwargs 在形参上*聚合, **聚合 在实参上*打散, **打散 ...

  4. vmware三种网络模式:桥接模式|仅主机|NAT模式

    VMware 网络模式 1. Bridged(桥接)桥接模式下,宿主机物理网卡(主机网卡)和虚拟网卡通过 VMnet0 虚拟交换机进行桥接,物理网卡和虚拟网卡在拓扑图上处于同等地位,物理网卡和虚拟网卡 ...

  5. openx -书表添加字段

    OpenX的版本是2.8.10.在数据表加完数据库之后,还不能读取和保存字段. OpenX使用scheme来 管理数据库表和字段, 修改数据库结构同时也要修改相关schema, 一个是etc/tabl ...

  6. linux的可中断sleep_on函数分析

    void interruptible_sleep_on (struct task_struct **p)// **p是个全局变量 { struct task_struct *tmp; if (!p)# ...

  7. JEECG 3.7.3 新春版本发布,企业级JAVA快速开发平台

    JEECG 3.7.3新春版本发布 -  微云快速开发平台 导读           ⊙精美Echart报表 ⊙二维码生成功能 ⊙Online接口改造采用JWT机制 ⊙智能菜单搜索 ⊙代码生成器模板优 ...

  8. day13-文件操作

    1.打开与关闭 1.1.open() close()我们使用 open() 函数打开文件.这个函数将返回一个文件对象,我们对文件的读写都将使用这个对象.open() 函数需要三个参数,第一个参数是文件 ...

  9. ThreadLoacl 小记

    参考地址: https://www.cnblogs.com/dolphin0520/p/3920407.html ThreadLoacl 本地线程变量 为线程创建一个副本, 一个内部类ThreadLo ...

  10. python 阿狸的进阶之路(4)

    装饰器 #1.开放封闭原则:对扩展开放,对修改是封闭#2.装饰器:装饰它人的,器指的是任意可调用对象,现在的场景装饰器->函数,被装饰的对象也是->函数#原则:1.不修改被装饰对象的源代码 ...