如何使用Python从网站上以编程方式下载Tableau csv文件?

问题描述 投票:0回答:1

我正在尝试使用Python以编程方式从以下站点下载数据:https://health.wyo.gov/publichealth/infectious-disease-epidemiology-unit/disease/novel-coronavirus/covid-19-testing-data/

在Tableau视图的右下角,有3个按钮:共享,下载和全屏。单击下载后,将转到另一个弹出窗口。然后,我想选择Crosstab,然后将您带到另一个弹出窗口,在这里我要选择Positivity,最后是要提供csv的Download

[从本质上讲,我已经设法浏览了一些iframe,但是由于按钮不包含ID /链接,因此我对单击的位置有些迷茫。

下面是一种方法的代码:

from selenium import webdriver

url = 'https://public.tableau.com/profile/melissa.taylor#!/vizhome/WyomingCOVID-19TestingDataDashboard/Dashboard1'

driver = webdriver.Chrome()
driver.get(url)
#elem = driver.switch_to.frame(driver.find_element_by_xpath('//iframe[contains(text(), "googletagmanager"]'))
try:
    time.sleep(4)
    iframe = driver.find_elements_by_tag_name('iframe')[0]
    driver.switch_to.default_content()

    driver.switch_to.frame(iframe)
    driver.find_elements_by_tag_name('iframe')
    #driver.find_element_by_id('download-ToolbarButton').click()

    print(driver.page_source)
finally:
    driver.quit()

下面是显示Tableau页面中三个按钮的HTML。

<div class="tab-nonVizItems tab-fill-right hideLabels"><div class="tabToolbarButton tab-widget undo disabled" role="button" data-tb-test-id="undo-ToolbarButton" id="undo-ToolbarButton" aria-disabled="true" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Undo"><span class="tabToolbarButtonImg tab-icon-undo"></span><span class="tabToolbarButtonText">Undo</span></div><div class="tabToolbarButton tab-widget redo disabled" role="button" data-tb-test-id="redo-ToolbarButton" id="redo-ToolbarButton" aria-disabled="true" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Redo"><span class="tabToolbarButtonImg tab-icon-redo"></span><span class="tabToolbarButtonText">Redo</span></div><div class="tabToolbarButton tab-widget revert disabled" role="button" data-tb-test-id="revert-ToolbarButton" id="revert-ToolbarButton" aria-disabled="true" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Reset"><span class="tabToolbarButtonImg tab-icon-revert"></span><span class="tabToolbarButtonText">Reset</span></div><div class="tabToolbarButton tab-widget share" role="button" data-tb-test-id="share-ToolbarButton" id="share-ToolbarButton" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Share"><span class="tabToolbarButtonImg tab-icon-share"></span><span class="tabToolbarButtonText">Share</span></div><div class="tabToolbarButton tab-widget download" role="button" data-tb-test-id="download-ToolbarButton" id="download-ToolbarButton" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Download"><span class="tabToolbarButtonImg tab-icon-download"></span><span class="tabToolbarButtonText">Download</span></div><div class="tabToolbarButton tab-widget enterFullscreen" role="button" data-tb-test-id="toggle-fullscreen-ToolbarButton" id="toggle-fullscreen-ToolbarButton" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Full Screen"><span class="tabToolbarButtonImg tab-icon-enterFullscreen"></span><span class="tabToolbarButtonText">Full Screen</span></div></div>
            <div></div></div><div class="tab-ReactView tab-toolbar-dialoghost"></div></div></div>

感谢您的帮助!

python selenium iframe automation tableau
1个回答
0
投票

尝试以下代码:

driver.get('https://public.tableau.com/profile/melissa.taylor#!/vizhome/WyomingCOVID-19TestingDataDashboard/Dashboard1')
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='Data Visualization']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".tab-icon-download"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Crosstab']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='positivity']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Download']"))).click()

正在导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
© www.soinside.com 2019 - 2024. All rights reserved.