FastAPI / main.py
andrewammann's picture
Update main.py
6624cc3 verified
raw
history blame
1.08 kB
from fastapi import FastAPI, HTTPException
import requests
from bs4 import BeautifulSoup
import pandas as pd
app = FastAPI()
def get_zillow_data(address):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
search_query = requests.utils.quote(address)
url = f'https://www.zillow.com/homes/{search_query}_rb/'
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
square_footage = 'N/A'
# Adjust the selectors based on Zillow's HTML structure
sqft_element = soup.find('span', {'data-testid': 'bed-bath-item'}, text=lambda x: 'sqft' in x)
if sqft_element:
square_footage = sqft_element.text.split('sqft')[0].strip()
return square_footage
@app.get("/zillow")
def read_zillow(address: str):
try:
sqft = get_zillow_data(address)
return {"address": address, "square_footage": sqft}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))