与硒一起使用代理

时间:2020-06-08 12:56:06

标签: python-3.x selenium-webdriver selenium-chromedriver proxies

因此,由于发出太多请求而使我的网站被IP阻止,我需要使用代理,但是在测试了一些方法之后,我发现只有一种似乎无效。

这是我当前设置代理的代码:

PROXY = '144.34.208.48:3128'
# If your using other OS please change this value to either
# Windows, MACOS, Linux or Raspberry
OS = 'Linux'
# Setup selenium
        proxy = Proxy()
        proxy.proxy_type = ProxyType.MANUAL
        proxy.autodetect = False
        proxy.httpProxy = proxy.sslProxy = proxy.socksProxy = PROXY

        options = webdriver.ChromeOptions()
        options.Proxy = proxy
        options.add_argument("ignore-certificate-errors")
        options.add_argument("user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4132.0 Safari/537.36")
        options.add_argument("referrer=https://google.com")
        options.add_argument("Accept-Encoding=gzip, deflate, br")
        options.add_argument("Accept-Language=en-US,en;q=0.9")
        options.add_argument("Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
        options.add_argument('--no-sandbox')
        if headless:
            options.add_argument('--headless')

        if OS == 'Linux':
            self.driver = webdriver.Chrome(executable_path=f'Drivers/Linux/chromedriver-{driver_version}', options=options)
        elif OS == 'Raspberry':
            self.driver = webdriver.Chrome(executable_path=f'/usr/lib/chromium-browser/chromedriver', options=options)
        elif OS == 'Windows':
            self.driver = webdriver.Chrome(executable_path=f'Drivers/Windows/chromedriver-{driver_version}', options=options)
        elif OS == 'MACOS':
            self.driver = webdriver.Chrome(executable_path=f'Drivers/MACOS/chromedriver-{driver_version}', options=options)
        else:
            exit(f'There is no OS called {OS}')

我目前正在使用的方式是打开浏览器,但使用的IP仍然是我的,而不是代理服务器。有什么办法做到这一点?

0 个答案:

没有答案