【问题标题】:Trouble getting few items from a webpage无法从网页中获取少量项目
【发布时间】:2018-06-01 03:22:54
【问题描述】:

我在 python 中结合 selenium 编写了一个脚本来解析网页中的一些项目。无论如何我无法让它工作。我追求的项目(也许)在iframe 内。我试图切换它,但没有任何效果。除了TimeoutException 到达我试图切换iframe 的行时,我什么也得不到。我怎样才能让它工作。提前致谢:

这里是网页链接:URL

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

url = "replace_with_above_url"

driver = webdriver.Chrome()
driver.get(url)
wait = WebDriverWait(driver, 10)

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "tradingview_fe623")))

for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".quick .apply-common-tooltip"))):
    print(item.text)

driver.quit()

我所追求的项目所在的元素:

<div class="quick">
    <span class="apply-common-tooltip">5</span>
    <span class="apply-common-tooltip">1h</span>
    <span class="apply-common-tooltip selected">1D</span>
    <span class="apply-common-tooltip">1M</span>
    <span class="apply-common-tooltip">1D</span>
</div>

这是我期望的输出(当我尝试使用 css 选择器获取它们时它在本地工作):

5
1h
1D
1M
1D

这是它在网络上的样子:

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver web-scraping


    【解决方案1】:

    位于 2 个嵌套 iframe 内的必需节点,因此您需要一一切换到它们。请注意,第二个动态生成的id/name。尝试替换

    wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "tradingview_fe623")))
    

    wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, ".abs")))
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id^='tradingview_']")))
    

    【讨论】:

    • 安德森先生,你简直是不可能的。是的,你成功了。
    猜你喜欢
    • 2019-12-27
    • 1970-01-01
    • 2020-01-28
    • 2018-06-10
    • 1970-01-01
    • 2012-03-18
    • 2015-06-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多