脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python利用selenium建立代理ip池访问网站的全过程

Python利用selenium建立代理ip池访问网站的全过程

2022-10-31 09:53HarryPoFly Python

selenium控制浏览器也是可以使用代理ip的,下面这篇文章主要给大家介绍了关于Python利用selenium建立代理ip池访问网站的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

一、使用selenium前?

1.安装selenium

pip install Selenium

2.安装浏览器驱动

Chrome驱动文件下载:点击下载

3.配置环境

1.将下载文件放进C:\Program Files (x86)\Google\Chrome\Application下就可以

Python利用selenium建立代理ip池访问网站的全过程

2.然后配置下系统变量:我的电脑–>属性–>系统设置–>高级–>环境变量–>系统变量–>Path,将“C:\Program Files (x86)\Google\Chrome\Application”目录添加到Path的值中。

Python利用selenium建立代理ip池访问网站的全过程

Python利用selenium建立代理ip池访问网站的全过程

Python利用selenium建立代理ip池访问网站的全过程

注:之后如果代码不能调起浏览器,重启电脑,再运行!!!

 

二、使用selenium

1.引入库

代码如下(示例):

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

2.完整代码

如果有多个代理ip可循环使用,防止被禁几率

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#以下ip使用自己可使用的代理IP
proxy_arr = [
   '--proxy-server=http://171.35.141.103:9999',
   '--proxy-server=http://36.248.132.196:9999',
   # '--proxy-server=http://125.46.0.62:53281',
   '--proxy-server=http://219.239.142.253:3128',
   '--proxy-server=http://119.57.156.90:53281',
   '--proxy-server=http://60.205.132.71:80',
   '--proxy-server=https://139.217.110.76:3128',
   '--proxy-server=https://116.196.85.150:3128'
]

chrome_options = Options()
proxy = random.choice(proxy_arr)  # 随机选择一个代理
print(proxy) #如果某个代理访问失败,可从proxy_arr中去除
chrome_options.add_argument(proxy)  # 添加代理
browser = webdriver.Chrome(options=chrome_options)
browser.get("http://httpbin.org/ip")
print(browser.page_source)

代码如下(示例):

Python利用selenium建立代理ip池访问网站的全过程

 

总结

到此这篇关于Python利用selenium建立代理ip池访问网站的文章就介绍到这了,更多相关Python selenium代理ip池访问网站内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/sinat_33801009/article/details/108383876

延伸 · 阅读

精彩推荐