I’m trying to web scrape an HVAC website. I want to put in a zipcode, hit the submit button, and then scrape the resulting information on the closest dealers. Here is the website if necessary.
My problem is that the zipcode I want is typed fine, but it won’t submit. After the zipcode submits, I want to check a div for the text on the number of details, and based on that choose what to scrape into a csv.
Simplified HTML of the button:
<div class="dl-zipcode-search__block">
<div class="zipcode-miles__wrap">
<div class="dl-searchbtn__wrap">
<button type="button" onclick="Search();">SEARCH</button>
</div>
</div>
</div>
Simplified HTML of the dealer count div:
<div class="dl-dealer-count__block">
<span>6 Dealers near <span id="searchedZip"></span></span>
</div>
I tried:
#function that checks the dealer count div
def is_multiple_dealers(driver):
dealer = driver.find_elements(By.CSS_SELECTOR, 'div.dl-dealer-count__block:nth-child(1) > span:nth-child(1)')
#checking if I am finding the dealer
print(dealer)
#boolean that returns true or false depending on if there are 0 results, and the text
for d in dealer:
if '0 dealers near' in d.text:
return False, d.text
break
else:
return True, d.text
break
break
#main excerpt
def main():
#webdriver setup code here
wait = WebDriverWait(driver, 10)
#webpage validation code here
#zipcode array
a = zip_arr
for x in range(len(a)):
#zipcode form submitting code here
#waiting until submit button is there then submitting
submit_zipcode = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.dl-searchbtn__wrap > button')))
submit_zipcode.click()
#scraping with beautiful soup
soup = BeautifulSoup(page_source,features="html.parser")
dealer_boolean, dealer_quantity_text = is_multiple_dealers(driver)
if dealer_boolean == True:
print(dealer_quantity_text)
#find data and add to array
else
#add an array entry as "NAN" for no dealers in the zipcode
driver.quit()
if __name__ == "__main__":
main()
Since I have been able to scrape another website with a zipcode entry and submit button fine I expected it to work.
Last three traceback lines:
File "C:Path_to_file_hereScrape_project.py", line 80, in main
dealer_boolean, dealer_quantity_text = is_multiple_dealers(driver)
TypeError: cannot unpack non-iterable NoneType object
The “print(dealer)” in the custom function shows up as
[]
so I’m guessing either my CSS Selector is wrong, or how I’m going about calling it? I also tried adding ‘wait’ as a parameter to the function to use ‘wait.until’ for the button to be visible but that didn’t help either.