Search code examples
pythonseleniumuser-agentproxies

Python script can't change user agent and proxies on Chrome


Actually, i have a script who start a chrome session (not headless). I want to change and rotate user agent and proxies, with fake_useragent and proxy_randomizer libraries. I've see on somes topics here how to make this, but not work on my chrome session. I use chromedriver 98.0.4758.82 and chrome browser 98.0.4758.82, and i don't have any error message on terminal. On "userAgent" string and "proxf" string, user agent and proxy are correctly rotate, i think an error is in argument on chrome option, but i don't know. Here is my script today:

from fake_useragent import UserAgent
from proxy_randomizer import RegisteredProviders
from proxy_randomizer.proxy import Anonymity
import requests

# URL
url = "https://www.carrefour.fr"

warnings.filterwarnings("ignore")

# Proxies
rp = RegisteredProviders()
rp.parse_providers()
#print(f"proxy: {rp.get_random_proxy()}")
for proxy in rp.proxies:
     proxies     = { "https": f"{proxy.ip_address}:{proxy.port}" }
     #response    = requests.get("http://google.com", proxies=proxies)
prox = rp.get_random_proxy()
proxip = prox.ip_address
proxpo = prox.port
proxf = "https://"+str(proxip)+":"+str(proxpo)
#print(prox)
#print(proxip)
#print(proxpo)
#print(proxf)

# Ouverture Chrome
options = Options()
ua = UserAgent()
userAgent = ua.random
print(userAgent)
#options.headless = True
driver = webdriver.Chrome(options=options)
options.add_argument(f'user-agent={userAgent}')
options.add_argument('--proxy-server=%s' % proxf)
driver.get(url)
driver.execute_script("return navigator.userAgent;")

Anyone have an idea why my user agent and proxies are not used on chrome session please? I think i have correct requests, but i don't know... Thanks for help! Bye


Solution

  • It's ok now, here is the correct code:

    from fake_useragent import UserAgent
    from proxy_randomizer import RegisteredProviders
    from proxy_randomizer.proxy import Anonymity
    import requests
    from fresh_useragent import UserAgent
    
    def getRandomUserAgent():
        lines = open('UAStrings.txt').read().splitlines()
        return random.choice(lines)
    ua = getRandomUserAgent()
    
    prox = rp.get_random_proxy()
    proxl = "http://"+str(prox)
    
    
    options.add_argument(f'user-agent={ua}')
    options.add_argument(f'--proxy-server={proxl}')