I am trying to webscrape, using python, a rental houses/apartment site pretty famous here in Brazil (5 andar).
I need to enter in each of the elements and scrape some information inside it. Any tips on how to do it? Since its an infinite scroll type of page?
OBS: By now I can already enter each element and scrape the data. my only problem is keep scrolling/ scraping the new data.
Here is the link for the site: https://www.quintoandar.com.br/alugar/imovel/sao-paulo-sp-brasil and a img of it
Here is what I have so far. It is already working on the first items
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import numpy as np
import pandas as pd
#Initializing the webdriver
options = webdriver.ChromeOptions()
#Change the path to where chromedriver is in your home folder.
path = 'chromedriver'
driver = webdriver.Chrome(executable_path=path, options=options)
driver.set_window_size(1600, 1024)
url = 'https://www.quintoandar.com.br/alugar/imovel/sao-paulo-sp-brasil'
driver.get(url)
time.sleep(5)
num_houses = 40
houses=[]
#Fix (scrolling the page a few items and going back to initial)
aux = driver.find_elements_by_xpath("//div[@class='sc-1qwl1yl-0 igVsBW']")
driver.execute_script("arguments[0].scrollIntoView();", aux[12])
time.sleep(1)
driver.execute_script("arguments[0].scrollIntoView();", aux[0])
time.sleep(1)
house_buttons = driver.find_elements_by_xpath("//div[@class='sc-1qwl1yl-0 igVsBW']")
for house_button in house_buttons:
if (not 'Sem tempo pra procurar' in house_button.text) and (not 'Ainda não encontrou seu lar' in house_button.text):
house_button.click()
#Wait for new tab
time.sleep(2)
#Switch to it
driver.switch_to.window(driver.window_handles[1])
#Wait page load its infos
time.sleep(4)
try:
title = driver.find_element_by_xpath("//h1[@class='sc-1q9n36n-0 ghXeyc sc-bdVaJa hgGleC']").text
address = driver.find_element_by_xpath("//p[@data-testid='listing-address-subtitle']").text
except:
title = address = np.nan
#General Infos
try:
infos = driver.find_elements_by_xpath("//div[@class='MuiGrid-root tptht-0 fAvqys MuiGrid-item MuiGrid-grid-xs-3 MuiGrid-grid-sm-3 MuiGrid-grid-md-1']")
size = infos[0].text
bedroom = infos[1].text
bathroom = infos[2].text
garage = infos[3].text
floor = infos[4].text
pet = infos[5].text
furniture = infos[6].text
subway = infos[7].text
except:
size = bedroom = bathroom = garage = floor = pet = furniture = subway = np.nan
#Price Infos
infos = driver.find_elements_by_xpath("//li[contains(@class, 'MuiListItem-root rf1epz-0')]")
for info in infos:
if 'Aluguel' in info.text: rent = info.text
elif 'Condomínio' in info.text: other = info.text
elif 'IPTU' in info.text: taxes = info.text
elif 'Seguro incêndio' in info.text: insurance = info.text
elif 'Taxa de serviço' in info.text: services = info.text
elif 'Total' in info.text: total = info.text
houses.append({
"Title":title,
"Address":address,
"Size":size,
"Bedroom":bedroom,
"Garage":garage,
"Floor":floor,
"Pet":pet,
"Size":size,
"Subway":subway,
"Rent":rent,
"Other":other,
"Taxes":taxes,
"Insurance":insurance,
"Services":services,
"Total":total
})
#Close Tab and go back to main
driver.close()
driver.switch_to.window(driver.window_handles[0])
time.sleep(.5)
