【发布时间】:2023-03-08 18:48:01
【问题描述】:
我正在尝试使用 python 进行网页抓取,这是一个在巴西非常有名的出租房屋/公寓网站 (5 andar)。
我需要输入每个元素并在其中抓取一些信息。 关于如何做到这一点的任何提示?因为它是无限滚动类型的页面?
OBS:现在我已经可以输入每个元素并抓取数据了。我唯一的问题是继续滚动/抓取新数据。
这是该网站的链接:https://www.quintoandar.com.br/alugar/imovel/sao-paulo-sp-brasil 还有一张图片
这是我目前所拥有的。它已经在处理第一个项目
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)
【问题讨论】:
-
我正在使用
html=driver.find_element_by_tag_name('html')html.send_keys(Keys.PAGE_UP)和html.send_keys(Keys.PAGE_DOWN)的组合做类似的事情。你有什么不想使用这些的理由吗? -
如果我废弃当前可用的元素,然后向下滚动页面,一些相同的元素仍然可见。所以他们被报废了两次
-
是的,我所做的是使用
.location['y']按“y”值过滤。您可以将它们存储在一组中。 -
我将尝试使用此“设置”提示...还有一个问题,您知道滚动此类页面的简单方法吗?哪里滚动不在实际页面中?
-
我也使用
driver.execute_script(f"window.scrollTo(0, {y})")到达我最近的位置。我不记得如何直接到达页面末尾。
标签: python selenium web-scraping infinite-scroll