Last active
January 9, 2022 05:32
-
-
Save GrantBirki/b03757ef7e62273d981b34b2f0c791b1 to your computer and use it in GitHub Desktop.
DownDetector Screenshot Scraper
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
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from PIL import Image | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.support.ui import WebDriverWait | |
from io import BytesIO | |
options = Options() | |
# Enable headless mode (optional) | |
# options.headless = True | |
# initializing webdriver for Chrome with our options | |
driver = webdriver.Chrome(options=options) | |
# Desired downdetector URL | |
url = "https://downdetector.com/status/escape-from-tarkov/" | |
# Open the website | |
driver.get(url) | |
# Wait for the chart to load | |
WAIT = 1 | |
WebDriverWait(driver, WAIT).until(EC.presence_of_element_located((By.ID, "chart-row"))) | |
# Get the chart element | |
s = driver.find_element(By.XPATH, "//body/div[3]/div[2]/div[1]/div[2]/div[1]") | |
# Get the sizes of the chart for cropping | |
location = s.location | |
size = s.size | |
x = location['x'] | |
y = location['y'] | |
h = location['y'] + size['height'] | |
w = location['x'] + size['width'] | |
# Save the chart screenshot to memory | |
p = driver.get_screenshot_as_png() | |
# Open the captured image to crop it | |
img_open = Image.open(BytesIO(p)) | |
# Crop the image | |
img_crop = img_open.crop((x, y, w, h)) | |
# Save the cropped image | |
img_crop.save("chart.png") | |
# close browser | |
driver.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This snippet of code will fetch the desired downdetector URL and grab a resulting screenshot of the chart like so:
To enable headless, mode simply uncomment the following line: