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

发表于:2021-5-18 09:58

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:HarryPoFly    来源:CSDN

  一、使用selenium
  1.安装selenium
  pip install Selenium
  2.安装浏览器驱动
  3.配置环境
  1).将下载文件放进C:\Program Files (x86)\Google\Chrome\Application下就可以。
  2).然后配置下系统变量:我的电脑–>属性–>系统设置–>高级–>环境变量–>系统变量–>Path,将“C:\Program Files (x86)\Google\Chrome\Application”目录添加到Path的值中。
  注:之后如果代码不能调起浏览器,重启电脑,再运行!!!
  二、使用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)
  代码如下(示例):

      本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号