Skip to content

Instantly share code, notes, and snippets.

@ian-quinn
Created May 13, 2021 11:55
Show Gist options
  • Save ian-quinn/2248ed920064a0a4d119056c147e207a to your computer and use it in GitHub Desktop.
Save ian-quinn/2248ed920064a0a4d119056c147e207a to your computer and use it in GitHub Desktop.
[vrvCrawler] Sample code to grab info from Darkin website
import os
import time
import threading
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
#from pyvirtualdisplay import Display
# display = Display(visible=0, size=(800, 800))
# display.start()
driver = webdriver.Chrome('chromedriver', chrome_options=chrome_options)
#driver.minimize_window()
wait = WebDriverWait(driver, 10)
def formation(attributeslist, filename):
attributeslist.pop(0)
info_formatted = ''
info_formatted += time.strftime("%Y/%m/%d %H:%M", time.localtime(time.time())) + ','
for room_index in range(len(attributeslist[0])):
for attribute_index in range(len(attributeslist)):
info_formatted += attributeslist[attribute_index][room_index] + ','
info_formatted.rstrip()
info_formatted += '\n'
with open(filename, 'a', encoding='utf-8-sig') as f:
f.write(info_formatted)
print('Data recorded at: ' + time.strftime("%m-%d %H:%M", time.localtime(time.time())))
def login():
print('Wait vrv.daikin-china.com.cn for response...')
driver.get('https://vrv.daikin-china.com.cn/#/login')
input = wait.until(EC.presence_of_element_located(
(By.XPATH, '//*[@id="username"]')))
input.send_keys('username_here')
input = wait.until(EC.presence_of_element_located(
(By.XPATH, '//*[@id="password"]')))
input.send_keys('password_here')
submit = wait.until(EC.element_to_be_clickable(
(By.XPATH, '//*[@id="loginSubmit"]')))
driver.implicitly_wait(5)
submit.click()
def grab_info():
driver.get('https://vrv.daikin-china.com.cn/#/monitoring-control/equipment-list')
panels, labels, temperatures, states, speeds, setpoints = [], [], [], [], [], []
driver.implicitly_wait(15)
print('Website loaded. Start to grab the information...')
try:
panels = driver.find_elements_by_css_selector('div.management-point__name')
driver.implicitly_wait(2)
for panel in panels:
driver.execute_script("arguments[0].click();", panel)
# panel.click() # malfuntion
driver.implicitly_wait(2)
labels.append(panel.text)
try:
temperatures.append(driver.find_element_by_css_selector('div.operation-panel__block.indoor-temperature >div:first-of-type')
.text)
except NoSuchElementException:
temperatures.append('')
try:
states.append(driver.find_element_by_css_selector('div.operation-panel__block.the-state')
.get_attribute('data-state'))
except NoSuchElementException:
states.append('')
try:
speeds.append(driver.find_element_by_css_selector('div.operation-panel__block.the-fan-speed >div:nth-of-type(2) >span:first-of-type')
.get_attribute('data-original-title'))
except NoSuchElementException:
speeds.append('')
try:
setpoints.append(driver.find_element_by_css_selector('div.operation-panel__block.set-temperature >div:nth-of-type(2)')
.text)
except NoSuchElementException:
setpoints.append('')
except Exception as e:
print(str(e))
attributeslist = [labels, temperatures, setpoints, states, speeds]
print(attributeslist)
return attributeslist
def record():
# customize your recording interval
threading.Timer(300, record).start()
attributeslist = grab_info()
if not attributeslist[0]:
login()
attributeslist = grab_info()
formation(attributeslist, filename)
login()
# create the header of the csv file
attributeslist = grab_info()
filename = time.strftime("%m-%d_%H-%M", time.localtime(time.time())) + '.csv'
with open(filename, 'w', encoding='utf-8-sig') as f:
row_label = ',' + ','.join([label + ',,,' for label in attributeslist[0]])
row_header = ',' + 'Temperature,Setpoint,State,Speed,' * len(attributeslist[0])
f.write(row_label + '\n' + row_header.rstrip() + '\n')
# continue recording
record()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment