Friday 9 July 2021

Using proxy with Chromedriver within Google Cloud Engine

I'm trying to use a proxy within Google Cloud Engine with chromedriver.

I've tried many solutions suggested (see below) but everytime the IP was the one on Google server.

Attempt 1:

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


chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--ignore-certificate-errors")

myproxy = '207.157.25.44:80'
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = myproxy
prox.ssl_proxy = myproxy

capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)

driver = webdriver.Chrome(options=chrome_options, 
    executable_path="/user/sebastien/chromedriver", 
    desired_capabilities=capabilities)
driver.get("https://www.whatismyip.com/")
get_location()


Attempt 2:

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


chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--ignore-certificate-errors")

myproxy = '207.157.25.44:80'
prefs = {}
prefs["network.proxy.type"] = 1
prefs["network.proxy.http"] = myproxy
prefs["network.proxy.ssl"] = myproxy

chrome_options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(options=chrome_options, 
    executable_path="/user/sebastien/chromedriver")
driver.get("https://www.whatismyip.com/")
get_location()

Attempt 3:

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


chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--ignore-certificate-errors")

myproxy = '207.157.25.44:80'
chrome_options.add_argument("--proxy-server=http://%s" % myproxy)

driver = webdriver.Chrome(options=chrome_options,
    executable_path="/user/sebastien/chromedriver")
driver.get("https://www.whatismyip.com/")
get_location()

None of them would reach the website with the desired IP.

Again, this issue is happening when running the code on GCP Compute Engine, Canonical, Ubuntu, 16.04 LTS, amd64 xenial.

Below the function to test the IP:

import json
from urllib.request import urlopen

def get_location(ip=False):
    if ip:
        html = urlopen(f"http://ipinfo.io/{str(ip).split(':')[0]}/json")
    else:
        html = urlopen("http://ipinfo.io/json")

    data = json.loads(html.read().decode('utf-8'))
    IP = data['ip']
    org = data['org']
    city = data['city']
    country = data['country']
    region = data['region']

    print('IP detail')
    print('IP : {4} \nRegion : {1} \nCountry : {2} \nCity : {3} \nOrg : {0}'.format(org, region, country, city, IP))

Thanks for reading !



from Using proxy with Chromedriver within Google Cloud Engine

No comments:

Post a Comment