2017-03-20 100 views
0

我正在尝试访问this网址,此处我必须根据价格/税收历史部分获取表格。以下是我的代码:Python Selenium:无法获取表格内容

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 
from time import sleep 
import os, sys 
from multiprocessing import Pool 
from selenium.webdriver import DesiredCapabilities 
from selenium.webdriver.support.ui import WebDriverWait 

driver = webdriver.Firefox() 
wait = WebDriverWait(driver, 5) 
driver.maximize_window() 
driver.get('https://www.zillow.com/homedetails/2114-Bigelow-Ave-N-Seattle-WA-98109/48749425_zpid/') 
sleep(10) 
p_history = driver.find_elements_by_css_selector('#tax-price-history table tr > td') 
    for p in p_history: 
     print(p.text) 

它不打印文本。

更新屏幕部分的要求:

enter image description here

更新#2

撞上了PhantomJS,在这里你可以在部分中看到装载机图像(滚动图像)

enter image description here

+0

你可以尝试用下面的查询选择,请注意,这是不可扩展并且只能在第一行工作,因为你需要做几个更改:document.querySelector('#tax-price-history tbody tr td:nth-​​child(3)') –

+0

@AnupamSaini首先,我是使用Python,第二我没有尝试这个,并没有工作 – Volatil3

+0

哪些文本,你正在尝试提取准确,有几个td没有任何tex吨,他们有跨度和更多的跨度里面,然后文本。 –

回答

2

您需要告诉硒使用WebDriverWaitexpected_conditions找到加载后的元素。

您需要引用页面加载时不存在的元素,但是一旦Ajax请求完成,就应该存在。看起来#tax-price-history table应该满足这个要求。

尝试:

from selenium.webdriver.support import expected_conditions as EC 
parent = wait.until(EC.presence_of_element_located((
    By.CSS_SELECTOR, '#tax-price-history table'))) 

p_history = parent.find_element_by_css_selector('td') 

如果在wait规定的期限内未找到该元素,你会得到一个错误

+0

'value = method(self._driver)TypeError:'list'object is not callable' – Volatil3

+0

ah。 python的语法完全不同。我会编辑答案 – jymbob