From 78b56c2cacaba78a51382c6b50a8d938a9971165 Mon Sep 17 00:00:00 2001 From: Zachary Hampton <69336300+ZacharyHampton@users.noreply.github.com> Date: Tue, 19 Sep 2023 09:25:08 -0700 Subject: [PATCH] - zillow location validation --- homeharvest/core/scrapers/zillow/__init__.py | 23 ++++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/homeharvest/core/scrapers/zillow/__init__.py b/homeharvest/core/scrapers/zillow/__init__.py index 7a84fd6..646872d 100644 --- a/homeharvest/core/scrapers/zillow/__init__.py +++ b/homeharvest/core/scrapers/zillow/__init__.py @@ -10,9 +10,10 @@ from ..models import Property, Address, ListingType, PropertyType class ZillowScraper(Scraper): def __init__(self, scraper_input): super().__init__(scraper_input) - self.listing_type = scraper_input.listing_type + if not self.is_plausible_location(self.location): raise NoResultsFound("Invalid location input: {}".format(self.location)) + if self.listing_type == ListingType.FOR_SALE: self.url = f"https://www.zillow.com/homes/for_sale/{self.location}_rb/" elif self.listing_type == ListingType.FOR_RENT: @@ -20,17 +21,15 @@ class ZillowScraper(Scraper): else: self.url = f"https://www.zillow.com/homes/recently_sold/{self.location}_rb/" - @staticmethod - def is_plausible_location(location: str) -> bool: - blocks = location.split() - for block in blocks: - if ( - any(char.isdigit() for char in block) - and any(char.isalpha() for char in block) - and len(block) > 6 - ): - return False - return True + def is_plausible_location(self, location: str) -> bool: + url = ('https://www.zillowstatic.com/autocomplete/v3/suggestions?q={' + '}&abKey=6666272a-4b99-474c-b857-110ec438732b&clientId=homepage-render').format( + location + ) + + response = self.session.get(url) + + return response.json()['results'] != [] def search(self): resp = self.session.get(self.url, headers=self._get_headers())