Web Scraping Using Beautiful Soup¶

Another practice at using beautiful soup (with a little help from ChatGPT).

Extracting cryptocurrency prices from this website: https://coinmarketcap.com/

Import Libraries¶

In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import requests
from bs4 import BeautifulSoup

Make Request and Get Soup Object¶

In [2]:
url = 'https://coinmarketcap.com/'
In [3]:
crypt_site = requests.get(url)
soup = BeautifulSoup(crypt_site.content, "html.parser")
In [4]:
# print(soup)

Extract the Data into an Array¶

In [5]:
table = soup.find("table")
data = []

rows = table.find_all("tr")

for row in rows:
    cells = row.find_all("td")
    if cells:
        row_data = [cell.text.strip() for cell in cells]
        # print(row_data)
        data.append(row_data)

Parse the Data Array To Display the Top 10 Most Expensive Currencies¶

In [6]:
parsed_data = []

sorted_data = sorted(data, key=lambda x: float(x[3].replace('$', '').replace(',', '')), reverse=True)
    
for row in sorted_data[:10]:  # Only iterate over the top ten rows
    currency = row[2].replace(row[1], '').replace(row[0], '').strip()
    price = row[3]
    parsed_data.append({'Currency': currency, 'Current Price': price})


df = pd.DataFrame(parsed_data)

df
Out[6]:
Currency Current Price
0 BitcoinBTC $69,745.60
1 EthereumETH $3,464.03
2 MakerMKR $3252.77
3 BNBBNB $612.45
4 BittensorTAO $585.65
5 Bitcoin CashBCH $585.53
6 GnosisGNO $368.87
7 SolanaSOL $168.14
8 MoneroXMR $134.45
9 AaveAAVE $113.45