【问题标题】:Set proxy server with selenium and Chrome使用 selenium 和 Chrome 设置代理服务器
【发布时间】:2018-07-07 23:26:28
【问题描述】:

如何使用 selenium 和 google chrome 使用代理服务器?我附上了代码,我不确定这是否会改变实际的代理服务器。

# selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random

PROXY ="88.157.149.250:8080";


chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
# //a[starts-with(@href, 'https://www.amazon.com/')]/@href
LINKS_XPATH = '//*[contains(@id,"result")]/div/div[3]/div[1]/a'
browser = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe",
                           chrome_options=chrome_options)
browser.get(
    'https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011')
links = browser.find_elements_by_xpath(LINKS_XPATH)
for link in links:
    href = link.get_attribute('href')
    print(href)

【问题讨论】:

    标签: python google-chrome selenium proxy


    【解决方案1】:
    from selenium import webdriver
    
    PROXY = "88.157.149.250:8080" # IP:PORT or HOST:PORT
    
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--proxy-server=%s' % PROXY)
    
    chrome = webdriver.Chrome(chrome_options=chrome_options)
    chrome.get("http://google.com")
    

    【讨论】:

    • 谢谢!如何测试ip是否被替换?
    • 是否有使用 Socks5 身份验证和用户名和密码的示例?尝试按以下格式设置代理时收到错误 ERR_NO_SUPPORTED_PROXIES:user:pass@IP/HOST:PORT。
    • 将此代码与此处spys.one/en/https-ssl-proxy 的任何免费 IP 一起使用,然后重定向到“我的 IP 页面是什么”,我的 IP 永远不会改变。
    • 如果代理服务器需要身份验证,则需要做更多工作才能连接到它,例如创建 Chrome 扩展程序。详情请见stackoverflow.com/a/35293284
    • 你应该将 chrome_options 更改为 Chrome 方法中的选项
    【解决方案2】:

    您可以打开页面https://www.whatismyip.com/my-ip-information/

    chrome.get("https://www.whatismyip.com/my-ip-information/")
    

    【讨论】:

      【解决方案3】:
      proxy = 192.168.22.1:8080
      if proxy != None:
          print('\nProxy ativado: ',proxy)
          #chrome_options.add_argument('--proxy-server=%s' % proxy)
          webdriver.DesiredCapabilities.CHROME['proxy'] = {
              "httpProxy": proxy,
              "ftpProxy": proxy,
              "sslProxy": proxy,
              "proxyType": "MANUAL",
      
          }
      
      webdriver.DesiredCapabilities.CHROME['acceptSslCerts']=True
      print(webdriver.DesiredCapabilities.CHROME)
      

      【讨论】:

        猜你喜欢
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多