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

2024-10-02 12:30:11 发布

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

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

在Tableau视图的右下角有3个按钮:共享、下载和全屏。单击下载后,您将进入另一个弹出窗口。然后我想选择交叉表,然后将您带到另一个弹出窗口,在那里我想选择积极性,最后,下载,它提供了一个csv

我基本上已经成功地浏览了一些iFrame,但由于按钮不包括id/link,我对单击的位置有点迷茫

下面是一种方法的一些代码:

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页面中3个按钮的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>

谢谢你的帮助


Tags: testdividdatadriverbuttonwidgettab
1条回答
网友
1楼 · 发布于 2024-10-02 12:30:11

请尝试以下代码:

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

相关问题 更多 >

    热门问题