Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save walkingpendulum/523284ed72d4724905cd27306a30510b to your computer and use it in GitHub Desktop.
Save walkingpendulum/523284ed72d4724905cd27306a30510b to your computer and use it in GitHub Desktop.
scraper.py
import os
import pathlib
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
def make_driver(headless: bool = True) -> WebDriver:
chrome_options = ChromeOptions()
if headless:
chrome_options.add_argument("--headless")
return webdriver.Chrome(options=chrome_options)
def authorize_at_ya(username: str, password: str, driver: WebDriver) -> None:
driver.get("https://passport.yandex.ru/auth")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "passp-field-login"))).send_keys(username)
driver.find_element_by_xpath('//button[normalize-space()="Войти"]').click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "passp-field-passwd"))).send_keys(password)
driver.find_element_by_xpath('//button[normalize-space()="Войти"]').click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "ProfileTile")))
if __name__ == '__main__':
driver = make_driver(headless=not bool(os.getenv('DISABLE_HEADLESS')))
username, password = os.environ["USERNAME"], os.environ["PASSWORD"]
authorize_at_ya(username=username, password=password, driver=driver)
driver.get("https://yandex.ru/map-constructor/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "button_id_import-button"))).click()
file_path = str(pathlib.Path("./map.geojson").resolve())
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@type='file']"))).send_keys(file_path)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "button_id_saveBtn"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "button_id_get-code-btn"))).click()
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "textarea_id_code-preview")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment