无法从zillow websi中获取一些元素

2024-05-02 15:10:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力搜集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格式

^{pr2}$

这是最外层的元素,但没有任何元素里面。还有尝试soup.find_all("table",class_ = "zsg-table yui3-toggle-content-minimized"),但没有结果。在


Tags: httpscom元素内容网站wwwtablefind
1条回答
网友
1楼 · 发布于 2024-05-02 15:10:49

您可以尝试等待生成所需的<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)

输出:

^{pr2}$

您也可以不使用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

相关问题 更多 >