andrewammann commited on
Commit
6624cc3
1 Parent(s): dd3ad16

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +29 -4
main.py CHANGED
@@ -1,7 +1,32 @@
1
- from fastapi import FastAPI
 
 
 
2
 
3
  app = FastAPI()
4
 
5
- @app.get("/")
6
- def read_root():
7
- return {"Hello": "World"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ import pandas as pd
5
 
6
  app = FastAPI()
7
 
8
+ def get_zillow_data(address):
9
+ headers = {
10
+ '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'}
11
+ search_query = requests.utils.quote(address)
12
+ url = f'https://www.zillow.com/homes/{search_query}_rb/'
13
+ response = requests.get(url, headers=headers)
14
+ soup = BeautifulSoup(response.content, 'html.parser')
15
+
16
+ square_footage = 'N/A'
17
+
18
+ # Adjust the selectors based on Zillow's HTML structure
19
+ sqft_element = soup.find('span', {'data-testid': 'bed-bath-item'}, text=lambda x: 'sqft' in x)
20
+ if sqft_element:
21
+ square_footage = sqft_element.text.split('sqft')[0].strip()
22
+
23
+ return square_footage
24
+
25
+ @app.get("/zillow")
26
+ def read_zillow(address: str):
27
+ try:
28
+ sqft = get_zillow_data(address)
29
+ return {"address": address, "square_footage": sqft}
30
+ except Exception as e:
31
+ raise HTTPException(status_code=500, detail=str(e))
32
+