Selenium+PhantomJS 设置代理IP

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cg_Amaz1ng/article/details/79777116
#coding:utf-8

from selenium import webdriver
from bs4 import BeautifulSoup
import urllib2
import re
import sys
import codecs
import json
import random
from time import sleep
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
#全局设置浏览器驱动
global driver

#调用api获取代理ip列表
def GetProxyIP(api):
     request = urllib2.Request(api)
     response = urllib2.urlopen(request)
     result = response.read()
     obj = json.loads(result)
     ipAddress = list(obj['msg'])
     return ipAddress

#调用PHANTOMJS设置代理IP
def SetProxyIP(count,ipAddress):
    address = ipAddress[count]
    ip = address['ip']
    port = address['port']
    script = "phantom.setProxy('{ip}',{port})".format(ip=ip,port=port)
    driver.execute('EXECUTE_PHANTOM_SCRIPT', {'script': script, 'args': []})

本地修改如下,参考知乎phantomjs selenium 如何动态修改代理? @张坤的回答

打开Python安装目录,找到\Lib\site-packages\selenium\webdriver\remote\remote_connection.py,在self._commands中最下面添加:

"EXECUTE_PHANTOM_SCRIPT":('POST','/session/$sessionId/phantom/execute')

重新编译remote_connection.py就可以了

展开阅读全文

没有更多推荐了,返回首页