def get_hdfcbank(url, bankInfo=None): bankName = bankInfo["NAME"] try: driver = utils.create_chromedriver() xpath = bankInfo["XPATH"] name = bankInfo["NAME"] if not xpath: print("[WARNING] Cannot find FxRate from {}".format(name)) return 0 utils.get_with_retry(driver, url) def get_text(dr): elem = dr.find_element(By.XPATH, xpath) return elem.text != "" WebDriverWait(driver, 10, 0.5).until(get_text) elem = driver.find_element_by_xpath(xpath) rateINRUSD = elem.text rateINRUSD = rateINRUSD.replace(",", "") rateINRUSD = locale.atof(rateINRUSD) driver.quit() return [(bankName, rateINRUSD)] except: traceback.print_exc() return [(bankName, 0)]
def get_uob(url, bankInfo=None): bankName = bankInfo["NAME"] try: driver = utils.create_chromedriver() utils.get_with_retry(driver, url) def get_text(dr): elem = dr.find_element(By.XPATH, "//tr[2]/td[2]") return elem != None WebDriverWait(driver, 10, 0.5).until(get_text) content = driver.page_source driver.quit() soup = BeautifulSoup(content, "html.parser") tds = soup.find_all("td") for idx, td in enumerate(tds): if td.text == "PHILIPPINES PESO": target_td = tds[idx + 2] rateSGDPHP = target_td.text rateSGDPHP = rateSGDPHP.replace(",", "") rateSGDPHP = locale.atof(rateSGDPHP) return [("{}, unit(100)".format(bankName), rateSGDPHP)] except: traceback.print_exc() return [(bankName, 0)]
def get_bkb(url, bankInfo=None): try: driver = utils.create_chromedriver() xpath = bankInfo["XPATH"] name = bankInfo["NAME"] if not xpath: print("[WARNING] Cannot find FxRate from {}".format(name)) return 0 utils.get_with_retry(driver, url) def get_text(dr): elem = dr.find_element(By.XPATH, xpath) return elem is not None WebDriverWait(driver, 10, 0.5).until(get_text) xpath2 = xpath + '/tbody/tr[3]/td[5]' elem = driver.find_element_by_xpath(xpath2) print(elem) fxrate = elem.text fxrate = fxrate.replace(",", "") driver.quit() return fxrate except: traceback.print_exc() return 0
def get_panin(url, bankInfo=None): bankName = bankInfo["NAME"] use_selenium = bankInfo['USE_SELENIUM'] try: if use_selenium: url = bankInfo["URL_FOR_XPATH"] xpath = '//*[@id="first"]/tr[1]/td[2]' driver = utils.create_chromedriver() if not xpath: print("[WARNING] Cannot find FxRate from {}".format(name)) return 0 utils.get_with_retry(driver, url) # Poll the element every 0.5s with a 10s timeout elem = None def get_text(dr): return dr.find_element(By.XPATH, xpath) is not None WebDriverWait(driver, 10, 0.5).until(get_text) elem = driver.find_element(By.XPATH, xpath) fxrate = elem.text fxrate = fxrate.split(",")[0].replace(".", "") driver.quit() return [("{}, unit(1)".format(bankName), fxrate)] else: # 撿到槍, 但 SSL Certification Failed (https://bugs.python.org/issue28150) # Potential solution: # 1) Install certifi to get Root CAs for verifying the identity of TLS hosts # 2) Do not verify the server's certificate. r = requests.get(url, verify=False) r.encoding = 'utf-8' soup = BeautifulSoup(r.text, "html.parser") tds = soup.find_all("td") content = ''.join([td.text for td in tds]) rateIDRUSD = content.split(":")[1] rateIDRUSD = rateIDRUSD.replace(",", "") rateIDRUSD = locale.atof(rateIDRUSD) return [("{}, unit(10)".format(bankName), rateIDRUSD)] except: traceback.print_exc() return [(bankName, 0)]
def get_pnb(url, bankInfo=None): bankName = bankInfo["NAME"] try: driver = utils.create_chromedriver() utils.get_with_retry(driver, url) def get_text(dr): elem = dr.find_element(By.TAG_NAME, "table") return elem != None WebDriverWait(driver, 10, 0.5).until(get_text) content = driver.page_source rateSGDPHP = content.partition("P1,000 = S$")[2].partition( "\n</pre>")[0] rateSGDPHP = rateSGDPHP.replace(",", "") rateSGDPHP = locale.atof(rateSGDPHP) driver.quit() return [("{}, unit(1000)".format(bankName), rateSGDPHP)] except: traceback.print_exc() return [(bankName, 0)]