您现在的位置是: 网站首页> 学习笔记> 爬虫 爬虫
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 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.
添加评论
点击排行
本栏推荐
标签云
热评文章
- django使用qq邮箱发送邮件
- mysql8设置数据库远程连接
- pip修改下载源为国内源
- win10看不到win7共享的文件夹的解决方法
- SQLyog连接 Mysql 8.0.11 报error no.1251- Client does not support authentic...
- 使用Oracel Net Nanager配置Oracle数据库远程访问
- 将anaconda的下载源切换为国内的源
- Python+selenium+firefox设置代理IP
- selenium+firefox+js实现动态设置firefox浏览器代理IP
- scrapy文件下载(高新技术企业认定网)
- Python调用JS代码
- Chrome浏览器的overrides的使用
站点信息
- 建站时间:2021-01-01
- 网站程序:Django 3.1.2
- 文章统计:53篇
- 文章评论:31条
- 统计数据:
#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!