pythonpandasseleniumcryptocurrencybscscan

Retrieving latest DEX Trades of a specified token address on BSCScan


I'm trying to get the list of DEX Trades from BSCScan. I don't think they have an API endpoint for this, so I tried using webscraping using selenium to retrieve the information.

import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

PATH = 'C:/Users/XX/Downloads/chromedriver_win32/chromedriver.exe'
driver = webdriver.Chrome(PATH)

driver.get('https://bscscan.com/token/0x20de22029ab63cf9a7cf5feb2b737ca1ee4c82a6#tokenTrade')
print(driver.title)

#search = driver.find_element_by_css_selector("table")

link = driver.find_element_by_css_selector("tbody")
print(link)
print(type(link))

driver.close()

I want to know how I can convert the 'link' into a pandas dataframe. If anyone knows of a smarter way to go about this or if there is an API which I can call to retrive the information, please let me know! Cheers!


Solution

  • There are total of 6 columns, you can use Selenium to scrape the data.

    See below, sample code and then we can use pandas to write into .csv file:

    PATH = 'C:/Users/XX/Downloads/chromedriver_win32/chromedriver.exe'
    driver = webdriver.Chrome(PATH)
    
    driver.maximize_window()
    driver.implicitly_wait(30)
    driver.get('https://bscscan.com/token/0x20de22029ab63cf9a7cf5feb2b737ca1ee4c82a6#tokenTrade')
    print(driver.title)
    wait = WebDriverWait(driver, 20)
    try:
      wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='btnCookie']"))).click()
    except:
      pass
    Txn_Hash = []
    Age = []
    Maker = []
    Taker = []
    Price = []
    DEX = []
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "dextrackeriframe")))
    wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Age"))).click()
    table_size = len(driver.find_elements(By.XPATH, "//thead[@class='thead-light']/following-sibling::tbody//tr"))
    print(table_size)
    j = 1
    for i in range(table_size):
        tnx_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/descendant::a[1]"))).text
        age_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[3]"))).text
        maker_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[4]"))).text
        Taker_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[6]"))).text
        Price_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[7]"))).text
        DEX_href = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[8]/img"))).get_attribute('src')
        Txn_Hash.append(tnx_text)
        Age.append(age_text)
        Maker.append(maker_text)
        Taker.append(Taker_text)
        Price.append(Price_text)
        DEX.append(DEX_href)
        j = j + 1
    
    print(Txn_Hash)
    print(Age)
    print(Maker)
    print(Taker)
    print(Price)
    print(DEX)
    
    data = {
         'Transaction_HashKey': Txn_Hash,
         'Age': Age,
         'Maker': Maker,
         'Taker' : Taker,
         'Price' :Price,
         'DEX' : DEX
        }
    df = pd.DataFrame.from_dict(data)
    df.to_csv('output.csv', index = 0)
    

    Imports :

    from selenium import webdriver
    import pandas as pd
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    Output :

    ['0x6a53b89b152c87d3685a38c5a5c19bfb32453cc623fe89ab173c8463b6b0e6e6', '0x8a4f7346a1d19532d120850ced34e4f5933f55f0acd6dae367d0a5809b609d63', '0x4dad847feae58c40a5b75f77923fb1c96145d28f07d0b470352ed7755f041c1e', '0x0c526ca7bff37da9aadf292126fea7884ffd77cba7d460a1472e9aed3013428f', '0x44ef571050a21e6735388845d57ded58fdf317149b497e92ce98dcc5baa7aadf', '0x7dfb9d3b85099393fb298f93a0d3b50a8f12152ee5fdca5b9c6a6f65f338f5da', '0x44871f9f5f1a425db7f80d16c8a9ff9534f7818383d756d66b6266f067b21cd7', '0x19fc930610a8cbe737bbc3d4dbcc36e0db7cd1a0f82d5eb719b9c020b7fcc0e4', '0x52ff7e0d0be3b31847486dae931082c0fc214a4c2b02ab42352c092150517850', '0xba76ea312b9b8175e6b06218f5716950e97586c23041a1437c120f4fa2a3e1d8', '0x541d1b03c401554081459bd47eaede29f93d061af72718a57542a00b0d1d2dce', '0xb7c5ae0f29496cea3c4f3e442eb0f21ba8477385bf1bb6b89eaca409e3175eed', '0x7a1235c0c38eba68d9355bbe731fd166c04465b88bcfe455724e205557fb1b8b', '0xfb41695cf7ae826d02db1fc12bf907c407e836df4ea3d11fc0ba915c949253d7', '0xa6c21b8ac550b9243190fe9a5b76e6ff2a400587880b9262091d28e3301d335c', '0x6522e970449f9cf82f0999499b5a3e8367d31ddee71d5ee79d6d56b9e957b3e1', '0x9bbc3a4770426a5f12c450cb53196ab972a6ceb5d14881f65c3dfbe3cbd645e6', '0x6cd417f5b0014e97d8d4547f06c630b4f31a2dd50790f60df7c18c64d1e35a88', '0x1ad4fda8ad5ea58dae3d875d37de4106b295cfca65f2d7e6664d552772dccdd1', '0x04bf4d08ebd75902adc8e4ab9c4407dcd9ccf8cf8f888e3e649c8cec5f45337b', '0x1d59b0c936b9f7b705790aaaee7ce489db36e17558cc9b63ed7a75ecb8ba9971', '0x52fd222811907b459ac56e0ec5bf27efc977e64d6fbcdf9baabe2263e357dd34', '0xabd360469b6845471565ad92be6313be716da0bcf7ea2e9fde4c417deef299c4', '0xc088290c0ae03a0cff4c9412d1e91337845f070995b6dad914dbbaad3fd5dbf7', '0x4599603ff85b1dcab982deb78e34819b375a5bfdd1ba1e5781b33ab356ad8c5a']
    ['9 mins ago', '9 mins ago', '11 mins ago', '14 mins ago', '16 mins ago', '18 mins ago', '26 mins ago', '26 mins ago', '27 mins ago', '27 mins ago', '28 mins ago', '29 mins ago', '32 mins ago', '33 mins ago', '34 mins ago', '37 mins ago', '41 mins ago', '43 mins ago', '43 mins ago', '44 mins ago', '45 mins ago', '48 mins ago', '48 mins ago', '50 mins ago', '50 mins ago']
    ['400.935687376294 USDC', '2000 USDC', '30.5170533895435 USDC', '80.9052749104618 CHESS', '798.905080279798 USDC', '10328.3266353108 USDC', '52.3940982126553 USDC', '1.83970166400884 CHESS', '1381.20296263661 CHESS', '996.732227659855 USDC', '1.10938188624252 CHESS', '733.276261427595 USDC', '250 CHESS', '118.41809384609 CHESS', '396.458359184589 CHESS', '91.8073445788602 CHESS', '37.8252819850425 CHESS', '8.46670714987361 USDC', '6476.27333270389 USDC', '106.39191585726 USDC', '3.86081522425666 USDC', '2779.63095555549 USDC', '129.876958430455 CHESS', '1112.2944509438 CHESS', '31.2951696372996 USDC']
    ['279.052065508868 CHESS', '1393.3544462088 CHESS', '21.2779425583375 CHESS', '115.459743125108 USDC', '557.169370926116 CHESS', '7235.67916136788 CHESS', '36.8598691618714 CHESS', '0.0065366541063913 WBNB', '1955.0061745213 USDC', '700.397738070921 CHESS', '0.00394852404186141 WBNB', '515.630197237616 CHESS', '353.694793816893 USDC', '167.570936536028 USDC', '561.186109255103 USDC', '129.989609753698 USDC', '53.5606402003673 USDC', '5.94934035709382 CHESS', '4562.71907835428 CHESS', '75.1563934355007 CHESS', '2.72744343816214 CHESS', '1965.87541797967 CHESS', '182.528727503485 USDC', '1564.33041187305 USDC', '22.1270395974478 CHESS']
    ['0.6960021 CHESS', '0.6966772 CHESS', '0.6972476 CHESS', '0.7007228 CHESS', '0.6974162 CHESS', '0.7005665 CHESS', '0.7035119 CHESS', '0.0035531 WBNB', '0.7064954 CHESS', '0.7026940 CHESS', '0.0035592 WBNB', '0.7031868 CHESS', '0.7068241 CHESS', '0.7066744 CHESS', '0.7064650 CHESS', '0.7062668 CHESS', '0.7062141 CHESS', '0.7026746 CHESS', '0.7045285 CHESS', '0.7064108 CHESS', '0.7064424 CHESS', '0.7072433 CHESS', '0.7115426 CHESS', '0.7110355 CHESS', '0.7070433 CHESS']
    ['https://bscscan.com/images/dex/pancake_32.png', 'https://bscscan.com/images/dex/pancake_32.png', 'https://bscscan.com/images/dex/pancake_32.png', '