PhantomJS Selenium 设置动态代理
java使用selenium,phantomjs进行模拟浏览器访问,请问如何在不重新启动phantomjs的情况下修改代理ip?
phantomjs 有一个setProxy的function,但是selenium找不到该如何调用该方法.
米扑代理,全球领导的代理品牌,专注代理行业近十年,提供开放、私密、独享代理,并可免费试用
米扑代理官网:https://proxy.mimvp.com
完整的代理示例,请见米扑代理的使用示例:https://proxy.mimvp.com/demo.php (Selenium Python)
更多的代理示例,见米扑代理的官方github:https://github.com/mimvp/mimvp-proxy-demo
本文中,测试的代理ip,全部来自米扑代理:https://proxy.mimvp.com
selenium对phantomjs的实现,可以这样做(Python代码):
# 不使用代理代打开ip138
browser=webdriver.PhantomJS(PATH_PHANTOMJS)
browser.get('https://proxy.mimvp.com/ip.php')
print('1: ',browser.session_id)
print('2: ',browser.page_source)
print('3: ',browser.get_cookies())
# 利用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('https://proxy.mimvp.com/ip.php')
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('https://proxy.mimvp.com/ip.php')
通过输出sessionid、cookies,可以发现,这两者都改变了。如果你要保留第一次访问的cookies,需要自己获取第一次cookies,然后在第二次访问前先delete_all_cookies, 然后add_cookie ,不要直接add_cookie,不然会产生两个name一样的cookie。
add_cookie时,phantomjs需要name/value/domain/path 写全(firefox只需要name和value),不然在2.11phantomjs中会发生异常"selenium.common.exceptions.WebDriverException: Message: You may only set cookies for the current domain"
在webdriver\remote\remote_connection\RemoteConnection里,self._commands下面添加 'PhantomScript': ('POST', '/session/$sessionId/phantom/execute'), 然后就可以执行setProxy了 script = 'phantom.setProxy("ip", port); bw.execute('PhantomScript', {'script': script, 'args': []})
selenium 官方文档都没有说明, 想必是不支持的, 不妨考虑直接用 phantomjs 运行 js 来实现相同功能.
phantomjs和selenium设置proxy、headers
#!/usr/bin/python
# -*- coding:utf-8 -*-
__author__ = 'Yl-Zh'
import random,headers,xmlParse
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import ProxyType
phantomjs_driver='C:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe'
ips=xmlParse.get_ip_port_from_xml('proxy_ip.xml')
def dynamic_load(url):
desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
# 从USER_AGENTS列表中随机选一个浏览器头,伪装浏览器
desired_capabilities["phantomjs.page.settings.userAgent"] = (random.choice(headers.my_headers))
# 不载入图片,爬页面速度会快很多
desired_capabilities["phantomjs.page.settings.loadImages"] = False
# 利用DesiredCapabilities(代理设置)参数值,重新打开一个sessionId,我看意思就相当于浏览器清空缓存后,加上代理重新访问一次url
# proxy = webdriver.Proxy()
# proxy.proxy_type = ProxyType.MANUAL
# proxy.http_proxy = random.choice(ips)
# proxy.add_to_capabilities(desired_capabilities)
# 打开带配置信息的phantomJS浏览器
# driver = webdriver.PhantomJS(executable_path=phantomjs_driver,desired_capabilities=desired_capabilities)
driver = webdriver.PhantomJS(executable_path=phantomjs_driver)
driver.start_session(desired_capabilities)
# 隐式等待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.get(url)
#next_page=driver.find_element_by_id (idd)#.get_attribute('href')
#driver.get(next_page)
#next_page
html=BeautifulSoup(driver.page_source,'xml').prettify()
print html
return html
if __name__=='__main__':
url='http://www.chnmuseum.cn/tabid/218/Default.aspx?DynastySortID=5'
dynamic_load(url)
参考推荐:
Python + Selenium + Firefox 使用代理 auth 的用户名密码授权
Python + Selenium + Chrome 使用代理 auth 的用户名密码授权
Selenium Webdriver 以代理proxy方式启动firefox,ie,chrome
Python + Selenium2 + Chrome 爬取网页
phantomjs selenium 如何动态修改代理 (知乎)
phantomjs和selenium设置proxy、headers (CSDN)
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2018-12-13 00:36:52
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!