간만에 노가다할 작업이 있어서 Python - Selenium을 사용했다.
pip3 install selenium
url = 'https://meshswap.fi/exchange/pool'
if __name__ == '__main__':
driver = webdriver.Chrome()
driver.get(url) // 해당 url로 크롬 브라우저를 열어줘
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
webdriver-manager 설치
pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
url = 'https://meshswap.fi/exchange/pool'
if __name__ == '__main__':
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get(url)
driver.get(url)을 입력하면 조작 가능한 크롬브라우저 창이 출력된다.
코드 해석: 브라우저가 열리고 XPATH(특정 데이터)까지 완전히 읽어올 수 있을 때까지(최대 5초)동안 기다리겠다.
wait = WebDriverWait(driver, 5)
element = wait.until(EC.element_to_be_clickable(
(By.XPATH, '//*[@id="app"]/main/section/section/section/article[2]/section[1]/div[2]/div[1]')))
저는 페어명을 가져오겠습니다.
바로 아래에 있는 페어도 가져와서 비교해보자
특정한 부분만 다르고 모두 같은 것을 알 수 있다.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.common.exceptions import NoSuchElementException
url = 'https://meshswap.fi/exchange/pool'
if __name__ == '__main__':
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get(url)
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#exchange-page > div > section > div.pool-main-body > section > article > div.pool-table__body > div:nth-child(1) > div.pool-table__col.pool-table__col--pair > div:nth-child(2) > p:nth-child(1) > strong')))
temp = []
result = driver.find_element(By.CSS_SELECTOR,
'#exchange-page > div > section > div.pool-main-body > section > article > div.pool-table__body > div:nth-child(1) > div.pool-table__col.pool-table__col--pair > div:nth-child(2) > p:nth-child(1) > strong'.format(2)).text
print(result)
try:
for i in range(1, 10):
if(i>1):
page = driver.find_element(By.CSS_SELECTOR,
'#exchange-page > div > section > div.pool-main-body > section > section > button.common-pager-button.common-pager-button--next').click()
time.sleep(1)
for j in range(1, 11):
pare = driver.find_element(By.CSS_SELECTOR,'#exchange-page > div > section > div.pool-main-body > section > article > div.pool-table__body > div:nth-child({0}) > div.pool-table__col.pool-table__col--pair > div:nth-child(2) > p:nth-child(1)'.format(j)).text
apy= driver.find_element(By.CSS_SELECTOR,'#exchange-page > div > section > div.pool-main-body > section > article > div.pool-table__body > div:nth-child({0}) > div.pool-table__col.pool-table__col--estimated > p.pool-table__col.pool-table__col--main-rate'.format(j)).text
tvl = driver.find_element(By.CSS_SELECTOR,'#exchange-page > div > section > div.pool-main-body > section > article > div.pool-table__body > div:nth-child({0}) > div.pool-table__col.pool-table__col--liquidity > p'.format(j)).text
temp.append(pare)
print('{0};{1};{2}'.format(pare, tvl, apy))
time.sleep(0.3)
except NoSuchElementException:
print(len(temp))
전체 페어의 명과 각 페어마다의 총 예치금, 1년 수익률을 가져왔다.