Spaces:
Sleeping
Sleeping
File size: 5,239 Bytes
66a6fee c53cf3d 66a6fee c53cf3d 8815db4 c53cf3d 8815db4 c53cf3d 8815db4 c53cf3d 331d078 8815db4 c53cf3d 8815db4 c53cf3d 8815db4 c53cf3d 8815db4 c53cf3d 331d078 66a6fee c53cf3d 66a6fee c53cf3d 8815db4 c53cf3d 8815db4 c53cf3d 8815db4 c53cf3d 8815db4 c53cf3d 8815db4 c53cf3d 8815db4 c53cf3d 66a6fee c53cf3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import gradio as gr
from scraper import FinancialScraper, format_financial_data
import time
# Initialize the scraper
scraper = FinancialScraper()
def get_stock_data(symbol, data_type="summary"):
"""Get stock data using web scraping"""
if not symbol or not symbol.strip():
return "β οΈ Please enter a valid stock symbol"
symbol = symbol.upper().strip()
try:
if data_type == "summary":
data = scraper.scrape_yahoo_summary(symbol)
elif data_type == "ratios":
data = scraper.scrape_key_statistics(symbol)
elif data_type == "comprehensive":
data = scraper.scrape_financial_highlights(symbol)
else:
return "β Invalid data type selected"
return format_financial_data(data)
except Exception as e:
return f"β Scraping Error: {str(e)}\n\nπ‘ Try again in a few seconds or check if the symbol is correct."
def get_multiple_stocks(symbols_text):
"""Get data for multiple stocks with rate limiting"""
if not symbols_text or not symbols_text.strip():
return "β οΈ Please enter at least one stock symbol"
try:
symbols = [s.strip().upper() for s in symbols_text.split(',') if s.strip()]
if not symbols:
return "β οΈ No valid symbols found"
if len(symbols) > 5:
return "β οΈ Please limit to 5 symbols or fewer to avoid rate limiting"
results = []
for symbol in symbols:
data = scraper.scrape_yahoo_summary(symbol)
formatted_result = format_financial_data(data)
results.append(formatted_result)
# Add extra delay between multiple requests
if len(symbols) > 1:
time.sleep(1)
return "\n" + "="*50 + "\n".join(results)
except Exception as e:
return f"β Error processing multiple stocks: {str(e)}"
# Create enhanced Gradio interface
with gr.Blocks(title="Financial Data Scraper", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π Stock Market Financial Data Scraper
**Get comprehensive financial data without API limits!**
β
Real-time stock prices and ratios
β
No rate limiting issues
β
PE/PB ratios, market cap, margins
β
Multiple data sources
""")
with gr.Tab("π Single Stock Analysis"):
with gr.Row():
with gr.Column(scale=2):
single_symbol = gr.Textbox(
label="Stock Symbol",
value="AAPL",
placeholder="Enter symbol (e.g., AAPL, MSFT, GOOGL)",
info="Enter any valid stock ticker symbol"
)
with gr.Column(scale=1):
data_type = gr.Dropdown(
choices=[
("Basic Summary", "summary"),
("Financial Ratios", "ratios"),
("Comprehensive Data", "comprehensive")
],
value="comprehensive",
label="Data Type"
)
single_btn = gr.Button("π Get Financial Data", variant="primary", size="lg")
single_output = gr.Textbox(
label="Financial Analysis Results",
lines=20,
max_lines=30,
show_copy_button=True
)
single_btn.click(
fn=get_stock_data,
inputs=[single_symbol, data_type],
outputs=single_output
)
with gr.Tab("π Multiple Stocks Comparison"):
multi_symbols = gr.Textbox(
label="Stock Symbols (comma separated)",
value="AAPL,MSFT,GOOGL",
placeholder="Enter up to 5 symbols: AAPL,MSFT,GOOGL,TSLA,AMZN",
info="Separate multiple symbols with commas (max 5 symbols)"
)
multi_btn = gr.Button("π Compare Stocks", variant="secondary", size="lg")
multi_output = gr.Textbox(
label="Multiple Stocks Comparison",
lines=25,
max_lines=40,
show_copy_button=True
)
multi_btn.click(
fn=get_multiple_stocks,
inputs=multi_symbols,
outputs=multi_output
)
with gr.Tab("βΉοΈ About"):
gr.Markdown("""
## How It Works
This tool scrapes financial data directly from Yahoo Finance using:
**π‘οΈ Anti-Detection Features:**
- Rotating User-Agents
- Rate limiting (1-2 second delays)
- Session management
- Error handling and retries
**π Available Data:**
- Current stock prices
- Market capitalization
- P/E, P/B, P/S ratios
- Profit margins
- Return on equity/assets
- 52-week ranges
- Enterprise value metrics
**π‘ Tips:**
- Use valid stock ticker symbols
- Allow a few seconds between requests
- Limit multiple stock queries to 5 symbols
""")
# Launch the app
demo.launch(show_error=True)
|