About | Features | Technologies | Requirements | Execution | License | Author
This project is a Python script that automates the retrieval of cryptocurrency prices from the web and stores the results in a DataFrame. The script utilizes the following libraries:
- pandas: for data manipulation.
- beautifulsoup4: for web scraping.
- requests: for making HTTP requests.
The script was developed using Jupyter Notebook with Python version 3.8.
The script operates as follows:
- Accesses the CoinMarketCap webpage.
- Collects the prices of specified cryptocurrencies.
- Stores the results in a DataFrame.
The code is divided into two main parts:
- Accessing the CoinMarketCap webpage
- Automating Price Retrieval
The first part of the code accesses the CoinMarketCap webpage to retrieve cryptocurrency information. The following code shows how the page is accessed:
import requests
from bs4 import BeautifulSoup
link = 'https://coinmarketcap.com/'
requisicao = requests.get(link)
site = BeautifulSoup(requisicao.text, 'html.parser')
The second part of the code automates the price retrieval for cryptocurrencies. The code operates as follows:
✔️ Feature 1: Accesses the CoinMarketCap page and collects data for listed cryptocurrencies.
✔️ Feature 2: Organizes the data into a pandas DataFrame.
The following code shows how the data is collected and organized:
import re
import pandas as pd
tbody = site.find('tbody')
linhas = tbody.find_all('tr')
moedas = {}
for linha in linhas:
try:
nome = linha.find(class_='kKpPOn').text
codigo = linha.find(class_='coin-item-symbol').text
valores = linha.find_all(string=re.compile('\$'))
preco = valores[0]
percentuais = linha.find_all(string=re.compile('%'))
for i, percentual in enumerate(percentuais):
if 'bQjSqS' in percentual.parent["class"]:
percentuais[i] = "-" + str(percentual)
var_1h = percentuais[0]
var_24h = percentuais[1]
var_7d = percentuais[2]
market_cap = valores[2]
volume = valores[3]
dic = {"nome": nome, "codigo": codigo, "preco": preco, "var_1h": var_1h, "var_24h": var_24h, "var_7d": var_7d,"market_cap": market_cap, "volume": volume}
moedas[nome] = dic
except AttributeError:
break
df = pd.DataFrame(moedas)
display(df)
The following tools were used in this project:
Before starting 🏁, make sure you have Git and Python installed.
# Clone the project
$ git clone https://github.com/gsoaresdz/coin-market-cap.git
# Run the script
$ jupyter notebook main.ipynb
- This script was developed for educational purposes. It is not recommended to use the script for commercial purposes without proper authorization from the websites.
- The script can be modified to meet different needs, such as changing the XPath selectors or adding new functionalities.
This project is under the MIT license. For more details, see the LICENSE file.
Made with ❤️ by gsoaresdz