【发布时间】:2018-12-22 12:24:33
【问题描述】:
我在 python 中结合 selenium 编写了一个脚本,以从其登陆页面抓取位于地图右侧区域的不同属性的链接。
当我从 chrome 手动单击每个块时,我会在新选项卡中看到包含此 /for_sale/ 部分的链接,而我的脚本获取的内容包含 /homedetails/。
我如何才能获得结果的数量(例如 153 套待售房屋)以及指向房产的正确链接?
到目前为止我的尝试:
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
link = "https://www.zillow.com/homes/33155_rb/"
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get(link)
itemcount = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#map-result-count-message h2")))
print(itemcount.text)
for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,".zsg-photo-card-overlay-link"))):
print(item.get_attribute("href"))
driver.quit()
当前输出之一:
https://www.zillow.com/homedetails/6860-SW-48th-Ter-Miami-FL-33155/44206318_zpid/
这样的预期输出之一:
https://www.zillow.com/homes/for_sale/Miami-FL-33155/house_type/44184455_zpid/72458_rid/globalrelevanceex_sort/25.776783,-80.256072,25.695446,-80.364905_rect/12_zm/0_mmm/
【问题讨论】:
-
至于 itemcount,我相信它是在页面加载后填充的,因此您需要某种延迟/睡眠。至于不正确的链接,您可以使用 css 选择器获取带有 homedetails 的链接,因此只需将其更改为您需要的任何内容。
标签: python python-3.x selenium selenium-webdriver web-scraping