【问题标题】:Can't scrape some elements off of zillow website无法从 zillow 网站上刮掉一些元素
【发布时间】:2017-10-09 21:54:49
【问题描述】:

我正在尝试抓取 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"),它没有产生任何元素。

【问题讨论】:

  • 能贴一下爬虫代码吗?
  • 相关代码在上面。

标签: python html selenium beautifulsoup screen-scraping


【解决方案1】:

您可以尝试等到所需的&lt;table&gt; 生成并变得可见:

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

【讨论】:

  • 我在这里尝试使用相同的技术link,但它似乎不起作用。我也尝试使用 id 和 element。以下是我尝试过的wait = WebDriverWait(browser, 20) table = wait.until(EC.visibility_of_element_located((By.XPATH, '//section[@id="yui_3_18_1_2_1494945298474_1290"]//table')))
  • id of section 是动态的(每次刷新页面时都会更改),因此您不能在 XPath 中使用它。试试//h2[text()="Price History"]/following-sibling::table
  • 我试过table = wait.until(EC.element_to_be_clickable( (By.XPATH, "//h3[text()='Price History']/following-sibling::table"))) print table.text它仍然没有获取任何东西。
  • 我的XPath 中的元素是h2,而你的元素是h3。编辑并重试
  • 是的,标题是 h3。我尝试了 h2 和 h3。
猜你喜欢
  • 2011-10-28
  • 2021-09-26
  • 1970-01-01
  • 2012-03-05
  • 2016-05-08
  • 2023-01-09
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
相关资源
最近更新 更多