selenium设置proxy、headers(phantomjs、Chrome、Firefox)
phantomjs
设置ip
方法1:
service_args = [
'--proxy=%s' % ip_html, # 代理 IP:prot (eg:192.168.0.28:808)
'--proxy-type=http’, # 代理类型:http/https
‘--load-images=no’, # 关闭图片加载(可选)
'--disk-cache=yes’, # 开启缓存(可选)
'--ignore-ssl-errors=true’ # 忽略https错误(可选)
]
driver = webdriver.PhantomJS(service_args=service_args)
方法2:
browser=webdriver.PhantomJS(PATH_PHANTOMJS)
# 利用DesiredCapabilities(代理设置)参数值,重新打开一个sessionId,我看意思就相当于浏览器清空缓存后,加上代理重新访问一次url
proxy=webdriver.Proxy()
proxy.proxy_type=ProxyType.MANUAL
proxy.http_proxy='1.9.171.51:800'
# 将代理设置添加到webdriver.DesiredCapabilities.PHANTOMJS中
proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS)
browser.start_session(webdriver.DesiredCapabilities.PHANTOMJS)
browser.get('http://1212.ip138.com/ic.asp')
print('1: ',browser.session_id)
print('2: ',browser.page_source)
print('3: ',browser.get_cookies())
还原为系统代理
# 还原为系统代理
proxy=webdriver.Proxy()
proxy.proxy_type=ProxyType.DIRECT
proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS)
browser.start_session(webdriver.DesiredCapabilities.PHANTOMJS)
browser.get('http://1212.ip138.com/ic.asp')
设置请求头
方法2
import random,requests,json
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import ProxyType
#随机获取一个ip
def proxies():
r = requests.get("http://120.26.166.214:9840/JProxy/update/proxy/scoreproxy")
rr = json.loads(r.text)
hh = rr["
print(hh)
return hh
ips =proxies()
#设置phantomjs请求头和代理方法一:
#-------------------------------------------------------------------------------------
# 设置代理
service_args = [
'--proxy=%s' % ips, # 代理 IP:prot (eg:192.168.0.28:808)
'--ssl-protocol=any', #忽略ssl协议
'--load - images = no', # 关闭图片加载(可选)
'--disk-cache=yes', # 开启缓存(可选)
'--ignore-ssl-errors=true' # 忽略https错误(可选)
]
#设置请求头
user_agent = (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"
)
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = user_agent
driver = webdriver.PhantomJS(executable_path=r"C:\soft\phantomjs-2.1.1-windows\bin\phantomjs.exe",
desired_capabilities=dcap,service_args=service_args)
driver.get(url='http://www.baidu.com')
page=driver.page_source
print(page)
#设置phantomjs请求头和代理方法二:
#-------------------------------------------------------------------------------------
desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
# 从USER_AGENTS列表中随机选一个浏览器头,伪装浏览器
desired_capabilities["phantomjs.page.settings.userAgent"] = (random.choice('请求头池'))
# 不载入图片,爬页面速度会快很多
desired_capabilities["phantomjs.page.settings.loadImages"] = False
# 利用DesiredCapabilities(代理设置)参数值,重新打开一个sessionId,我看意思就相当于浏览器清空缓存后,加上代理重新访问一次url
proxy = webdriver.Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = random.choice('ip池')
proxy.add_to_capabilities(desired_capabilities)
phantomjs_driver = r'C:\phantomjs-2.1.1-windows\bin\phantomjs.exe'
# 打开带配置信息的phantomJS浏览器
driver = webdriver.PhantomJS(executable_path=phantomjs_driver,desired_capabilities=desired_capabilities)
driver.start_session(desired_capabilities)
driver.get(url='http://www.baidu.com')
page=driver.page_source
print(page)
# 隐式等待5秒,可以自己调节
driver.implicitly_wait(5)
# 设置10秒页面超时返回,类似于requests.get()的timeout选项,driver.get()没有timeout选项
# 以前遇到过driver.get(url)一直不返回,但也不报错的问题,这时程序会卡住,设置超时选项能解决这个问题。
driver.set_page_load_timeout(20)
# 设置10秒脚本超时时间
driver.set_script_timeout(20)
#翻页命令
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
firefox
import time
from selenium.webdriver.common.proxy import*
myProxy = '202.202.90.20:8080'
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': ''
})
profile = webdriver.FirefoxProfile()
if proxy:
profile = get_firefox_profile_with_proxy_set(profile, proxy)
if user_agent:
profile.set_preference("general.useragent.override", user_agent)
driver=webdriver.Firefox(proxy=proxy,profile=profile)
driver.get('https://www.baidu.com')
time.sleep(3)
driver.quit()
firefox无头模式
from selenium import webdriver
# 创建的新实例驱动
options = webdriver.FirefoxOptions()
#火狐无头模式
options.add_argument('--headless')
options.add_argument('--disable-gpu')
# options.add_argument('window-size=1200x600')
executable_path='./source/geckodriver/geckodriver.exe'
driver_path = webdriver.Firefox(firefox_options=options,executable_path=executable_path)
chrome
# !/usr/bin/python
# -*- coding: utf-8 -*-
from selenium import webdriver
# 进入浏览器设置
options = webdriver.ChromeOptions()
#谷歌无头模式
options.add_argument('--headless')
options.add_argument('--disable-gpu')
# options.add_argument('window-size=1200x600')
# 设置中文
options.add_argument('lang=zh_CN.UTF-8')
# 更换头部
options.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"')
#设置代理
if proxy:
options.add_argument('proxy-server=' + proxy)
if user_agent:
options.add_argument('user-agent=' + user_agent)
browser = webdriver.Chrome(chrome_options=options)
url = "https://httpbin.org/get?show_env=1"
browser.get(url)
browser.quit()
selenium设置chrome--cookie
# !/usr/bin/python
# -*- coding: utf-8 -*-
from selenium import webdriver
browser = webdriver.Chrome()
url = "https://www.baidu.com/"
browser.get(url)
# 通过js新打开一个窗口
newwindow='window.open("https://www.baidu.com");'
# 删除原来的cookie
browser.delete_all_cookies()
# 携带cookie打开
browser.add_cookie({'name':'ABC','value':'DEF'})
# 通过js新打开一个窗口
browser.execute_script(newwindow)
input("查看效果")
browser.quit()
selenium设置chrome-图片不加载
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {
'profile.default_content_setting_values': {
'images': 2
}
}
options.add_experimental_option('prefs', prefs)
browser = webdriver.Chrome(chrome_options=options)
# browser = webdriver.Chrome()
url = "http://image.baidu.com/"
browser.get(url)
input("是否有图")
browser.quit()
selenium设置proxy、headers(phantomjs、Chrome、Firefox)的更多相关文章
- selenium设置代理,基于chrome浏览器
工作中遇到需要对项目中使用的selenium设置代理,跟大家分享一下. 1.下载chromeDriver:http://chromedriver.storage.googleapis.com/inde ...
- Selenium 设置浏览器下载 Firefox 和Chrome
当我们在使用Selenium运行自动化测试时,偶尔需要用到下载功能,但浏览器的下载可能会弹出下载窗口,或者下载路径不是我们想要保存的位置,所以在通过Selenium启动浏览器时需要做相关的设置,将使这 ...
- selenium设置chrome和phantomjs的请求头信息
selenium设置chrome和phantomjs的请求头信息 出于反爬虫也好-跳转到手机端页面也好都需要设置请求头,那么如何进行呢? 目录 一:selenium设置phantomjs请求头: ...
- 爬虫 selenium + phantomjs / chrome
selenium 模块 Web自动化测试工具, 可运行在浏览器,根据指定命令操作浏览器, 必须与第三方浏览器结合使用 安装 sudo pip3 install selenium phantomjs 浏 ...
- [转]爬虫 selenium + phantomjs / chrome
目录 selenium 模块 安装 phantomjs 浏览器 安装 chromedriver 接口 安装 对比两个接口 整合使用 基本实例 常用属性方法 定位节点 节点操作 其他操作 实例解析 - ...
- selenium设置Chrome
关闭图片 from selenium import webdriver options = webdriver.ChromeOptions() prefs = { 'profile.default_c ...
- selenium之 驱动环境配置chrome、firefox、IE
讲起动态网页获取我们一定会用到selenium,至于selenium在各种语言的开发代码很多,但是在我们兴致勃勃找了很多代码,要运行的时候,编译器只会给我们抛出异常,因为我们没有配置好环境.下面我将为 ...
- web自动化测试(2):选择selenium优势?与PhantomJS/QTP/Monkey对比
上篇 <web自动化测试(1):再谈UI发展史与UI.功能自动化测试>,自动化测试工具众多, PC端常用的功能自动化测试工具 Selenium:开源工具集,用于回归功能测试或者系统用例说明 ...
- 实现代理设置proxy
用户在哪些情况下是需要设置网络代理呢? 1. 内网上不了外网,需要连接能上外网的内网电脑做代理,就能上外网:多个电脑共享上外网,就要用代理: 2.有些网页被封,通过国外的代理就能看到这被封的网站:3. ...
随机推荐
- obj-c属性的新的特性
在以前的objc中我们必须在接口中定义属性对应的实例方法,然后在实现文件中"同步"该属性,如下代码: @interface Foo:NSObject{ NSString *name ...
- 关于Html5发展和应用前景
现在的HTML5就像当年崭露头角时的Ajax,有人在做,但不知道叫它什么.最近,苹果在 HTML5上大做文章,而著名的Web设计师Eric Meyer则提出了Web Stacks的概念.Alex Ke ...
- ruby创建某些“关键字”方法别名的语法
begin和end是ruby的关键字,但是Range中也有名称为begin和end的实例方法.现在问题来了:怎么创建它们的别名方法? 如果用class Range;alias begin_x begi ...
- centos 7下安装python 3.6笔记
每次在centos上安装python 3都需要重新查资料,这次索性自己记下笔记. 首先安装gcc yum -y install gccyum install zlib-devel./configure ...
- 解决Visual Studio 2017隐藏“高级保存选项”命令
Visual Studio提供高级保存选项功能,它能指定特定代码文件的编码规范和行尾所使用的换行符.在Visual Studio 2017中,该命令没有默认显示在“文件”菜单中.用户需要手工设置,才能 ...
- javascript数组特性
数组是一段线性分配的内存, 它通过整数计算偏移并访问其中的元素. 数组是一种性能出色的数据结构. 1.数组字面量 数组字面量提供了一种非常方便地创建新数组的表示法. 多个用逗号分隔的值的表达式. 数组 ...
- search a 2D matrix(在二维数组中搜索一个元素)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- MySql 动态语句
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
- centos安装nginx(针对一哥们的博客进行的详细补充(用红色字体标出了补充部分))
centos安装nginx 原文地址:http://www.cnblogs.com/leechenxiang/p/5402960.html 一.什么是nginx 是一个C语言开发的HTTP反向代理服务 ...
- Windows10 ubuntu子系统的启用即基础配置
Windows 10 在一周年更新后,本身集成一个不带有图形界面的ubuntu 14.04系统了,大大方便了Linux开发,并且本身使用很方便,像我这种Windows死忠,只会在Linux下跑一下一定 ...