一,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. Submline Text 3插件sublimeTmpl添加新模板

    1.安装 一般安装Package Control 2.插件 添加模板 1).进入Preferences->Browse Packages->SublimeTmpl->template ...

  2. python-对象方法、静态方法、类方法

    #-*- coding:utf-8 -*- #本次学习:对象方法.静态方法.类方法 class SeniorTestingEngineer: #属性--只能对象来调用self.salary work_ ...

  3. AJAX发送 PUT和DELETE请求参数传递注意点,了解一下

    ajax发送put 和 delete 请求时,需要传递参数,如果参数在url地址栏上,则可以正常使用, 如果在 data:中需要传递参数,(浏览器会使用表单提交的方式进行提交) 则需要注意此时应作如下 ...

  4. django-权限验证场景

    1.需要登录才能够访问的验证 from django.contrib.auth.decorators import login_required # 登录装饰器 # method_decorator ...

  5. hive随机采样

    hive> select * from account limit 10;OKaccount.accountname     account.accid   account.platid  ac ...

  6. [记录] CSS 多行文本超出部分省略

    如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览 <p style="width: 30 ...

  7. 如何在notepad++实现代码自动化排版(调用Astyle)

    我先介绍这个怎么在notepad++中调用原版的astyle的方法. 在notepad++:运行或是F5, 在输入框中选择astyle.exe所在的目录,什么你没有astyle,下载地址https:/ ...

  8. STM32F103C8开发板原理图和管脚图

  9. centos下查看python的安装目录

    直接用python命令,打印sys的path即可: >>> import sys >>> print(sys.path) ['', '/usr/local/lib/ ...

  10. flex 上下div固定, 中间div自适应

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...