【发布时间】:2017-08-01 12:17:48
【问题描述】:
我在 python 中结合 selenium 编写了一个脚本,以从位于网页 (finance.yahoo) 中的某个表中抓取数据。但是,当我执行它时,我得到一个错误。我不知道我是否犯了任何错误。 FYC,我目前写的都贴在下面了。
我正在尝试的脚本:
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
driver = webdriver.Chrome()
driver.get("https://finance.yahoo.com/")
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "table.tbl tbody")))
items = driver.find_element_by_css_selector("table.tbl tbody")
list_of_data = [[item.text for item in data.find_elements_by_css_selector('td')]
for data in items.find_elements_by_css_selector('tr')]
for tab_data in list_of_data:
print(tab_data)
driver.quit()
我的脚本抛出的错误:
line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
表格数据所在元素的部分部分:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="tbl">
<tbody>
<tr><th class="pr">Loan Type</th><th class="rate">Today</th><th class="ch">Change</th><th class="lw">Last Week</th></tr>
<tr class="">
<td class="pr"><a target="_top" rel="nofollow" href="https://finance.yahoo.com/rates">30 yr fixed</a></td>
<td class="rate">3.82%</td>
<td class="ch"><div class="arrow-up"></div></td>
<td class="lw">3.80%</td>
</tr>
<tr class="bk ">
<td class="pr"><a target="_top" rel="nofollow" href="https://finance.yahoo.com/rates/mortgage/15-year-fixed">15 yr fixed</a></td>
<td class="rate">3.01%</td>
<td class="ch"><div class="arrow-down"></div></td>
<td class="lw">3.05%</td>
</tr>
</tbody>
</table>
【问题讨论】:
-
增加超时时间
-
感谢 Gaurang Shah 的建议。增加了时间并执行但没有运气。仍然有同样的错误。
-
检查元素是否在任何框架内
-
我在
https://finance.yahoo.com/上找不到(By.CSS_SELECTOR, "table.tbl tbody")的任何元素,谢谢。您的确切手动步骤是什么?谢谢
标签: python python-3.x selenium selenium-webdriver web-scraping