DEV Community

Cover image for Finding tokens on Ethereum blockchain with Python
codesharedot
codesharedot

Posted on

Finding tokens on Ethereum blockchain with Python

In the crypto space it seems there are many coins. You may know that 99% of those digital coins run on distributed networks. Is there a network for each of those coins?

No, that is not the case. Many of the so called coins run on the Ethereum blockchain. This means they are in fact tokens.

So which coins have their own network? So far it's Bitcoin and Ethereum. You can see which tokens run on Ethereum using etherscan

Can we grab a list of tokens with Python? Yes, we can. But you need to know the Python basics

Beautifulsoup

Import a bunch of modules

#!/usr/bin/python3
import time
import os
import json
import requests
from bs4 import BeautifulSoup
import csv
import sys
from time import sleep
from time import gmtime, strftime
import re

Then grab the HTML data

#!/usr/bin/python3
r  = requests.get("https://etherscan.io/tokens")
data = r.text

Then parse the table and grab the first column. Because there is to much text in the column, we stop grabbing the text after the ) token. We remove the HTML tags with regular expressions.

#!/usr/bin/python3
soup = BeautifulSoup(data, "html.parser")
table = soup.find('table', attrs={ "class" : "table"})
for row in table.find_all('tr'):
    tag = row.findAll('td')
    if len(tag) >= 7:
        token = tag[1]
        token = re.sub('<[^<]+?>', '', str(token))
        if ")" in token:
            token = token[0:token.index(")")+1]
            print(token)

This only grabs the coins on page 1, but you can grab them from all pages by wrapping it in a function.

Git

If you don't want to copy and paste, you can just git clone the repository. Git is a tool used by developers, so if you are a software developer I recommend to install git.

In the terminal you can type:

git clone https://github.com/codesharedot/ethereum-tokens.git

If you don't want git, you can also grab the code with the link below.

Related links:

Top comments (0)