您现在的位置是: 网站首页> 学习笔记> 爬虫 爬虫

selenium+firefox+js实现动态设置firefox浏览器代理IP

2021-01-03 [selenium] [设置代理] 3990人已围观

from selenium import webdriver
import time,random
from redis import ConnectionPool, StrictRedis


REDIS_IPPool_ARGS = {
   'host': '192.168.2.99',
   'port': 6379,
   'db': 14    # 储存的IP格式:http://xxx.xxx.xxx.xxx:xxx
}

pool_ip = ConnectionPool(**REDIS_IPPool_ARGS)
conn_ip = StrictRedis(connection_pool=pool_ip)

def get_ip():
    # 随机取IP
    ip = conn_ip.lindex("proxies", random.choice(range(0, conn_ip.llen("proxies")))).decode('utf-8')
    return ip.split(':')[1][2:], ip.split(':')[-1]

ip_addr, ip_port = get_ip()
print(ip_addr, ip_port)

js_base='''var pf = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

    pf.setIntPref("network.proxy.type", 1);

    pf.setCharPref("network.proxy.http", "{0}");

    pf.setIntPref("network.proxy.http_port", {1});

    pf.setCharPref("network.proxy.ssl", "{2}");

    pf.setIntPref("network.proxy.ssl_port", {3});'''

driver = webdriver.Firefox()
driver.get("about:config")    # 如果不请求这个页面,js中会抛出Components.classes未定义的异常
driver.execute_script(js_base.format(ip_addr,ip_port,ip_addr,ip_port))

# 测试ip是否设置成功
driver.get('https://www.baidu.com')
driver.find_element_by_xpath('//input[@id="kw"]').send_keys('ip')
driver.find_element_by_xpath('//input[@id="su"]').click()
time.sleep(6)

ip_addr, ip_port = get_ip()
print(ip_addr, ip_port)

driver.get("about:config")
driver.execute_script(js_base.format(ip_addr,ip_port,ip_addr,ip_port))    # 切换IP

# 测试ip是否设置成功
driver.get('https://www.baidu.com')
driver.find_element_by_xpath('//input[@id="kw"]').send_keys('ip')
driver.find_element_by_xpath('//input[@id="su"]').click()
time.sleep(10)

driver.quit()

文章评论

#2023-11-18 05:39 @ D Change:

Having read this I thought it was extremely informative. I appreciate you spending some time and effort to put this short article together. I once again find myself spending a significant amount of time both reading and commenting. But so what, it was still worthwhile!

#2023-11-18 22:41 @ D Change:

Hello! I could have sworn I've been to this blog before but after browsing through some of the post I realized it's new to me. Nonetheless, I'm definitely happy I found it and I'll be bookmarking and checking back frequently!

#2023-11-20 06:42 @ d-change.net:

Hi! Someone in my Facebook group shared this website with us so I came to give it a look. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers! Outstanding blog and fantastic design and style.

添加评论





本栏推荐

站点信息

  • 建站时间:2021-01-01
  • 网站程序:Django 3.1.2
  • 文章统计:53篇
  • 文章评论:31条
  • 统计数据