Python使用selenium建立代理ip池访问网站

文章目录

  • 一、使用selenium前?
    • 1.安装selenium
    • 2.安装浏览器驱动
    • 3.配置环境
  • 二、使用selenium
    • 1.引入库
    • 2.完整代码


一、使用selenium前?

1.安装selenium

pip install Selenium

2.安装浏览器驱动

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

3.配置环境

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

Python使用selenium建立代理ip池访问网站_第1张图片

2.然后配置下系统变量:我的电脑–>属性–>系统设置–>高级–>环境变量–>系统变量–>Path,将“C:\Program Files (x86)\Google\Chrome\Application”目录添加到Path的值中。
Python使用selenium建立代理ip池访问网站_第2张图片
Python使用selenium建立代理ip池访问网站_第3张图片

Python使用selenium建立代理ip池访问网站_第4张图片

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

二、使用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池访问网站_第5张图片

你可能感兴趣的:(python,python,selenium)