File size: 1,084 Bytes
6624cc3
 
 
 
dd3ad16
 
 
6624cc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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))