So I created a web scraper code using Python for Forex as a resume project. To achieve that functionality, I utilized the BeautifulSoup library and Alpha Vantage API. However, in my terminal, it keeps on saying the error - "alpha_vantage.foreignexchange" could not be resolved. I don't understand how it can't work considering that my codebase is built normally.
This is my codebase:
import requests
from bs4 import BeautifulSoup
from alpha_vantage.foreignexchange import ForeignExchange
def get_forex_data(url):
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
# Extract forex information from the webpage using BeautifulSoup
# Modify the below code to extract specific data you need
# Example:
forex_info = soup.find('div', {'class': 'forex-info'})
if forex_info:
data = forex_info.get_text()
return data
else:
print("Forex information not found on the webpage.")
else:
print("Failed to fetch data. Check the URL or your internet connection.")
return None
def save_to_txt(filename, data):
with open(filename, 'w', encoding='utf-8') as file:
file.write(data)
def get_realtime_exchange_rate(api_key, from_currency, to_currency):
try:
fx = ForeignExchange(key=api_key)
data, _ = fx.get_currency_exchange_rate(from_currency=from_currency, to_currency=to_currency)
return data['5. Exchange Rate']
except Exception as e:
print(f"Error fetching real-time exchange rate: {e}")
return None
def get_historical_exchange_rates(api_key, from_currency, to_currency, start_date, end_date):
try:
fx = ForeignExchange(key=api_key)
data, _ = fx.get_currency_exchange_rate(from_currency=from_currency, to_currency=to_currency, date_format='YYYY-MM-DD', outputsize='full')
rates = data['Time Series FX (Daily)']
historical_rates = {}
for date, rate_info in rates.items():
if start_date <= date <= end_date:
historical_rates[date] = rate_info['4. close']
return historical_rates
except Exception as e:
print(f"Error fetching historical exchange rates: {e}")
return None
if __name__ == "__main__":
# Functionality 1: Currency Pair Selection
from_currency = input("Enter the base currency (e.g., USD): ").upper()
to_currency = input("Enter the target currency (e.g., EUR): ").upper()
# Functionality 2: Data Validation and Error Handling
url = input("Enter the URL for forex information: ")
forex_data = get_forex_data(url)
if forex_data:
save_to_txt('forex_info.txt', forex_data)
print("Forex information saved to 'forex_info.txt'.")
# Functionality 3: Real-Time Exchange Rate
alpha_vantage_api_key = # Replace 'YOUR_ALPHA_VANTAGE_API_KEY'
if alpha_vantage_api_key:
exchange_rate = get_realtime_exchange_rate(alpha_vantage_api_key, from_currency, to_currency)
if exchange_rate:
print(f"Real-Time Exchange Rate ({from_currency}/{to_currency}): {exchange_rate}")
else:
print("Please provide a valid Alpha Vantage API key to fetch real-time exchange rate.")
# Functionality 4: Historical Exchange Rates
start_date = input("Enter the start date (YYYY-MM-DD) for historical exchange rates: ")
end_date = input("Enter the end date (YYYY-MM-DD) for historical exchange rates: ")
historical_rates = get_historical_exchange_rates(alpha_vantage_api_key, from_currency, to_currency, start_date, end_date)
if historical_rates:
print("Historical Exchange Rates:")
for date, rate in historical_rates.items():
print(f"{date}: {rate}")
else:
print("Failed to fetch historical exchange rates.")
But I think that I somehow can't access alpha vantage's capabilities.