Last active
May 8, 2024 01:55
-
-
Save wilmeragsgh/b31c54ba0c28467e7fe799aee8568f4e to your computer and use it in GitHub Desktop.
Get value from different currencies to Venezuelan Bolivar from BCV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Get value from different currencies to Venezuelan Bolivar from BCV""" | |
import requests | |
from pprint import pprint | |
from bs4 import BeautifulSoup | |
# Most params | |
url = 'https://www.bcv.org.ve/' | |
exchanges_pairs_selector = {'class_': 'row recuadrotsmc'} | |
exchanges_date_selector = {'name': 'span', 'class_': 'date-display-single'} | |
# URL get call | |
response = requests.get(url) | |
# Check for successful response. | |
if response.status_code == 200: | |
# Parse the HTML content using BeautifulSoup. | |
soup = BeautifulSoup(response.content, 'html.parser') | |
# Find all elements with the class "item". | |
items = soup.find_all(**exchanges_pairs_selector) | |
exchanges = [] | |
exchange_date = ( | |
soup | |
.find(**exchanges_date_selector) | |
.attrs['content'] | |
) | |
# Util functions | |
clean_string = lambda x: x.text.strip() | |
clean_value = lambda x: float(clean_string(x).replace(',', '.')) | |
for item in items: | |
exchanges.append({ | |
"exchange_name": clean_string(item.select('span')[0]), | |
"exchange_value": clean_value(item.select('strong')[0]), | |
"exchange_date": exchange_date | |
}) | |
pprint(exchanges) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment