Skip to content

Instantly share code, notes, and snippets.

@evan-kolberg
Last active July 25, 2022 00:53
Show Gist options
  • Save evan-kolberg/0cbbed03a51180ad081e1a866161df5a to your computer and use it in GitHub Desktop.
Save evan-kolberg/0cbbed03a51180ad081e1a866161df5a to your computer and use it in GitHub Desktop.
from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from random_user_agent.user_agent import UserAgent
from random_user_agent.params import SoftwareName, OperatingSystem
from selenium_stealth import stealth
from bs4 import BeautifulSoup
import logging
logging.getLogger('WDM').setLevel(logging.NOTSET)
software_names = [SoftwareName.CHROME.value]
operating_systems = [OperatingSystem.WINDOWS.value, OperatingSystem.LINUX.value]
user_agent_rotator = UserAgent(software_names=software_names, operating_systems=operating_systems, limit=100)
user_agent = user_agent_rotator.get_random_user_agent()
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument(f'--user-agent={user_agent}')
options.add_argument('--window-size=960,540')
options.add_argument('--incognito')
driver = Chrome(service=Service(ChromeDriverManager().install()), options=options)
stealth(driver, languages=['en-US', 'en'], vendor='Google Inc.',
platform='Win32', webgl_vendor='Intel Inc.',
renderer='Intel Iris OpenGL Engine', fix_hairline=True)
driver.set_window_position(0, 0, windowHandle='current')
print(f'User Agent: {user_agent}')
driver.get('https://google.com')
soup = BeautifulSoup(driver.page_source, 'html.parser')
print('\n' + soup.find('img')['alt'], 'is working!')
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment