繁体   English   中英

如何以编程方式从带有 Python 的网站下载 Tableau csv 文件?

[英]How can I programmatically download Tableau csv file from website with Python?

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

Tableau 视图的右下角有 3 个按钮:共享、下载和全屏 单击“下载”后,您将被带到另一个弹出窗口。 然后我想要 select交叉表,然后将您带到另一个弹出窗口,我想要 select积极性,最后,下载提供 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()

下面是 HTML,它显示了 Tableau 页面中的 3 个按钮。

 <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>

谢谢你的帮助!!

试试下面的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM