Another practice at using beautiful soup (with a little help from ChatGPT).
Extracting cryptocurrency prices from this website: https://coinmarketcap.com/
import pandas as pd
import matplotlib.pyplot as plt
import requests
from bs4 import BeautifulSoup
url = 'https://coinmarketcap.com/'
crypt_site = requests.get(url)
soup = BeautifulSoup(crypt_site.content, "html.parser")
# print(soup)
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)
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
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 |