| 22 | |
| 23 | |
| 24 | class HouseData: |
| 25 | def __init__(self): |
| 26 | self.driver = webdriver.Chrome(executable_path=chrome_path) |
| 27 | # Making a request to the website |
| 28 | self.web = requests.get( |
| 29 | "https://www.zillow.com/homes/for_rent/1-_beds/?searchQueryState=%7B%22pagination%22%3A%7B%7D%2C%22usersSearchTerm%22%3Anull%2C%22mapBounds%22%3A%7B%22west%22%3A-122.56276167822266%2C%22east%22%3A-122.30389632177734%2C%22south%22%3A37.69261345230467%2C%22north%22%3A37.857877098316834%7D%2C%22isMapVisible%22%3Atrue%2C%22filterState%22%3A%7B%22fr%22%3A%7B%22value%22%3Atrue%7D%2C%22fsba%22%3A%7B%22value%22%3Afalse%7D%2C%22fsbo%22%3A%7B%22value%22%3Afalse%7D%2C%22nc%22%3A%7B%22value%22%3Afalse%7D%2C%22cmsn%22%3A%7B%22value%22%3Afalse%7D%2C%22auc%22%3A%7B%22value%22%3Afalse%7D%2C%22fore%22%3A%7B%22value%22%3Afalse%7D%2C%22pmf%22%3A%7B%22value%22%3Afalse%7D%2C%22pf%22%3A%7B%22value%22%3Afalse%7D%2C%22mp%22%3A%7B%22max%22%3A3000%7D%2C%22price%22%3A%7B%22max%22%3A872627%7D%2C%22beds%22%3A%7B%22min%22%3A1%7D%7D%2C%22isListVisible%22%3Atrue%2C%22mapZoom%22%3A12%7D", |
| 30 | headers=HEADERS, |
| 31 | ) |
| 32 | self.my_html = self.web.content |
| 33 | |
| 34 | self.soup = BeautifulSoup(self.my_html, "html.parser") |
| 35 | |
| 36 | # This function help in selection the specific elemen(address,link)t from the web page |
| 37 | def my_css(self, css_class): |
| 38 | return ( |
| 39 | css_class is not None |
| 40 | and "StyledPropertyCardDataArea-c11n-8-89-0__sc-yipmu-0 gZUDVm property-card-link" |
| 41 | in css_class |
| 42 | ) |
| 43 | |
| 44 | # This function help in selection the specific elemen(price)t from the web page |
| 45 | def my_price(self, css_class): |
| 46 | return ( |
| 47 | css_class is not None |
| 48 | and "PropertyCardWrapper__StyledPriceLine-srp__sc-16e8gqd-1 iMKTKr" |
| 49 | in css_class |
| 50 | ) |
| 51 | |
| 52 | # This Function find data we need and add them to the lists |
| 53 | def scrape(self): |
| 54 | # print(self.soup.prettify()) |
| 55 | address = self.soup.find_all(class_=self.my_css) |
| 56 | price = self.soup.find_all(class_=self.my_price) |
| 57 | for i in address: |
| 58 | ADDRESS.append(i.text) |
| 59 | LINK.append(f'https://www.zillow.com{i.get("href")}') |
| 60 | |
| 61 | for j in price: |
| 62 | PRICE.append(j.text) |
| 63 | |
| 64 | # This function use selenium and fill form with the data we scrape |
| 65 | def form(self): |
| 66 | for i in range(len(ADDRESS)): |
| 67 | self.driver.get(FORM) |
| 68 | time.sleep(1) |
| 69 | ad = self.driver.find_element( |
| 70 | By.XPATH, |
| 71 | "/html/body/div/div[2]/form/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input", |
| 72 | ) |
| 73 | ad.send_keys(ADDRESS[i]) |
| 74 | |
| 75 | pr = self.driver.find_element( |
| 76 | By.XPATH, |
| 77 | "/html/body/div/div[2]/form/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input", |
| 78 | ) |
| 79 | pr.send_keys(PRICE[i]) |
| 80 | |
| 81 | li = self.driver.find_element( |