Last active
March 17, 2018 12:39
-
-
Save imfht/a9fc151a6ed8ad2b319d2d97d3bb18bd to your computer and use it in GitHub Desktop.
selenium chrome headless add user_agent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import base64 | |
import sys | |
import uuid | |
from io import BytesIO | |
from PIL import Image | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
CHROME_PATH = '/usr/bin/google-chrome' | |
CHROMEDRIVER_PATH = '/usr/bin/chromedriver' | |
WINDOW_SIZE = "1920,1080" | |
chrome_options = Options() | |
chrome_options.add_argument("--headless") | |
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE) | |
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36' | |
chrome_options.add_argument('user-agent=%s' % user_agent) | |
chrome_options.binary_location = CHROME_PATH | |
domain = sys.argv[1] | |
output = str(uuid.uuid5(uuid.NAMESPACE_URL, domain.encode('utf-8'))) | |
driver = webdriver.Chrome( | |
executable_path=CHROMEDRIVER_PATH, | |
chrome_options=chrome_options | |
) | |
output_dir = './shoot/' | |
driver.get(domain) | |
data = driver.get_screenshot_as_base64() | |
agent = driver.execute_script("return navigator.userAgent") | |
print agent | |
driver.close() | |
im = Image.open(BytesIO(base64.b64decode(data))) | |
im.save(output_dir + output + '.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment