selenium使用代理爬取数据

代理

因为chrome的代理使用目前还有一些问题,所以本文全部用的Firefox

http协议

host = "127.0.0.1"
port = 9999
profile = webdriver.FirefoxOptions()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.http', host)  # IP为你的代理服务器地址:如‘127.0.0.0’,字符串类型
profile.set_preference('network.proxy.http_port', port)  # PORT为代理服务器端口号:如,9999,整数类型
browser = webdriver.Firefox(options=profile)

https协议

host = "127.0.0.1"
port = 9999
profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.http', "%s" % host)
profile.set_preference('network.proxy.http_port', port)
profile.set_preference('network.proxy.ssl', "%s" % host)
profile.set_preference('network.proxy.ssl_port', port)
profile.update_preferences()
browser = webdriver.Firefox(firefox_profile=profile)

拨号上网

还有一种情况就是可以通过拨号上网的方式实现不停地切换ip,从而防止ip地址被封。

# 先将网络断开
os.system('Rasdial 宽带连接 /DISCONNECT')
# 然后重新连接网络
os.system('Rasdial 宽带连接 宽带用户名 宽带密码')
# 判断网络是否重连成功
# 如果有网络,则res为0,否则为1
res = os.system('ping www.baidu.com')
if not res:
	print('网络重连成功')
else:
	print('网络重连失败')
发布了26 篇原创文章 · 获赞 1 · 访问量 6960

猜你喜欢

转载自blog.csdn.net/qq_39800434/article/details/104941769