无法从zillow网站上删除一些元素

时间:2017-05-11 03:50:04

标签: python html selenium beautifulsoup screen-scraping

我正试图抓住zillow网站的内容。

前 - https://www.zillow.com/homedetails/689-Luis-Munoz-Marin-Blvd-APT-508-Jersey-City-NJ-07310/108625724_zpid/

问题是我无法清除价格和税收历史的内容。 我认为它们是加载页面时加载的javascript元素,因此尝试使用selenium但我仍然无法获取它们。 以下就是我的尝试。

代码

phistory = soup.find("div",{"id": "hdp-price-history"})
print phistory

HTML

<div class="loading yui3-widget yui3-async-block yui3-complaintstable yui3-hdppricehistory yui3-hdppricehistory-content" id="hdp-price-history">
  div class="zsg-content-section zsg-loading-spinner_lg"></div>
</div>

这是最外面的元素,但内部没有任何元素。还尝试了soup.find_all("table",class_ = "zsg-table yui3-toggle-content-minimized"),但没有产生任何元素。

1 个答案:

答案 0 :(得分:3)

您可以尝试等到生成所需的<table>并变为可见:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

driver.get("https://www.zillow.com/homedetails/689-Luis-Munoz-Marin-Blvd-APT-508-Jersey-City-NJ-07310/108625724_zpid/")
table = wait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//div[@id="hdp-price-history"]//table')))
print(table.text)

输出:

DATE EVENT PRICE $/SQFT SOURCE
05/03/17 Listed for sale $750,000+159% $534 KELLER WILLIAM...
06/15/11 Sold $290,000-38.3% $206 Public Record
10/14/05 Sold $470,000 $334 Public Record

您也可以在不使用BeautifulSoup的情况下解析它,例如

print(table.find_element_by_xpath('.//td[text()="Listed for sale"]/following::span').text)

输出:

$750,000

print(table.find_element_by_xpath('.//td[text()="Sold"]/following::span').text)

输出:

$290,000