- zillow location validation

pull/8/head
Zachary Hampton 2023-09-19 09:25:08 -07:00
parent 087854a688
commit 78b56c2cac
1 changed files with 11 additions and 12 deletions

View File

@ -10,9 +10,10 @@ from ..models import Property, Address, ListingType, PropertyType
class ZillowScraper(Scraper): class ZillowScraper(Scraper):
def __init__(self, scraper_input): def __init__(self, scraper_input):
super().__init__(scraper_input) super().__init__(scraper_input)
self.listing_type = scraper_input.listing_type
if not self.is_plausible_location(self.location): if not self.is_plausible_location(self.location):
raise NoResultsFound("Invalid location input: {}".format(self.location)) raise NoResultsFound("Invalid location input: {}".format(self.location))
if self.listing_type == ListingType.FOR_SALE: if self.listing_type == ListingType.FOR_SALE:
self.url = f"https://www.zillow.com/homes/for_sale/{self.location}_rb/" self.url = f"https://www.zillow.com/homes/for_sale/{self.location}_rb/"
elif self.listing_type == ListingType.FOR_RENT: elif self.listing_type == ListingType.FOR_RENT:
@ -20,17 +21,15 @@ class ZillowScraper(Scraper):
else: else:
self.url = f"https://www.zillow.com/homes/recently_sold/{self.location}_rb/" self.url = f"https://www.zillow.com/homes/recently_sold/{self.location}_rb/"
@staticmethod def is_plausible_location(self, location: str) -> bool:
def is_plausible_location(location: str) -> bool: url = ('https://www.zillowstatic.com/autocomplete/v3/suggestions?q={'
blocks = location.split() '}&abKey=6666272a-4b99-474c-b857-110ec438732b&clientId=homepage-render').format(
for block in blocks: location
if ( )
any(char.isdigit() for char in block)
and any(char.isalpha() for char in block) response = self.session.get(url)
and len(block) > 6
): return response.json()['results'] != []
return False
return True
def search(self): def search(self):
resp = self.session.get(self.url, headers=self._get_headers()) resp = self.session.get(self.url, headers=self._get_headers())