Last active
April 22, 2020 21:57
-
-
Save csaybar/a945f343cbac1e047897fcb23c349c0b to your computer and use it in GitHub Desktop.
Barja desinventar selenium
This file contains hidden or 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
| import os | |
| import ee | |
| import ast | |
| import sys | |
| import json | |
| import time | |
| import pickle | |
| import requests | |
| import selenium | |
| import subprocess | |
| from bs4 import BeautifulSoup | |
| import re | |
| from selenium import webdriver | |
| from selenium.webdriver import Chrome | |
| from selenium.webdriver.chrome.options import Options | |
| from selenium.webdriver.common.keys import Keys | |
| from selenium.common.exceptions import TimeoutException | |
| from selenium.webdriver.support.ui import WebDriverWait | |
| from selenium.webdriver.support import expected_conditions as EC | |
| from selenium.webdriver.common.by import By | |
| from requests_toolbelt import MultipartEncoder | |
| def get_serial(selenium_path_driver): | |
| desinventar = 'https://www.desinventar.net/DesInventar/main.jsp?' | |
| peru_en = 'countrycode=per&lang=EN' | |
| authorization_url = desinventar + peru_en | |
| options = Options() | |
| driver = Chrome(executable_path=selenium_path_driver, | |
| chrome_options=options) | |
| driver.get(authorization_url) | |
| landslide = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="desinventar"]/table/tbody/tr/td/table[3]/tbody/tr[1]/td[1]/select/option[7]'))) | |
| landslide.click() | |
| rain = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="desinventar"]/table/tbody/tr/td/table[3]/tbody/tr[1]/td[5]/select/option[20]'))) | |
| rain.click() | |
| view_data = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="di_tabs"]/div/ul/li[3]/a'))) | |
| view_data.click() | |
| return driver | |
| def coordinates_by_page(driver): | |
| lon_lat = [] | |
| for x in range(200): | |
| print('processing row:' + str(x)) | |
| xpath = '/html/body/table[2]/tbody/tr[%s]/td[1]/a' % (x + 3) | |
| serial = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,xpath))) | |
| serial.click() | |
| soup = BeautifulSoup(driver.page_source, "html.parser") | |
| m = re.search('LatLng(\(.+?\))', soup.text) | |
| latlng = eval(m.group(1)) | |
| lon_lat.append(latlng) | |
| xpath_back = '//*[@id="di_tabs"]/div/ul/li[3]/a' | |
| serial = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,xpath_back))) | |
| serial.click() | |
| return lon_lat | |
| dirname = '/home/aybarpc01/.config/earthengine/' | |
| selenium_path_driver = os.path.join(dirname, 'chromedriver') | |
| driver = get_serial(selenium_path_driver) | |
| lon_lat_list = coordinates_by_page(driver) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment