Search code examples
python-3.xweb-scrapingtableau-api

Scraping a public tableau dashboard with custom tables


This is a follow-up question to How to scrape a public tableau dashboard? and the use of this impressive Tableau scraper library. How could I adapt this library to this Tableau that has the ability to allow for custom tables? Is it possible for me to turn the drop-down values into parameters I can combine to create custom CSV outputs? The goal is to automate downloading custom tables created by this Tableau.

https://tabexternal.dshs.texas.gov/t/THD/views/Deaths/Deaths?%3Aembed=y&showTabs=true&%3Adisplay_count=n&%3AshowVizHome=n&%3Aorigin=viz_share_link

What I enter:

   from tableauscraper import TableauScraper as TS

url = 'https://tabexternal.dshs.texas.gov/t/THD/views/Deaths/Deaths?%3Aembed=y&showTabs=true&%3Adisplay_count=n&%3AshowVizHome=n&%3Aorigin=viz_share_link'
ts = TS()
ts.loads(url)
wb = ts.getWorkbook()
data = wb.getCsvData(sheetName='Number of Deaths')

print(data)

What I get:

Empty DataFrame
Columns: [{"timeStamp":"2022-10-08 15:58:16.057", errorResponseType:"Generic", errorExtras:"(Y0GeF0ZgfP4LOiCpE7cUNwAAAIY, 3:0)"}]
Index: []

I'm trying to follow what's provided on the library page.

Any help would be appreciated!!

edit-1(added image): enter image description here


Solution

  • The following code will return approx 7 dataframes:

    from tableauscraper import TableauScraper as TS
    
    url = 'https://tabexternal.dshs.texas.gov/t/THD/views/Deaths/Deaths?%3Aembed=y&showTabs=true&%3Adisplay_count=n&%3AshowVizHome=n&%3Aorigin=viz_share_link'
    
    ts = TS()
    ts.loads(url)
    workbook = ts.getWorkbook()
    
    for t in workbook.worksheets:
        print(f"worksheet name : {t.name}")
        display(t.data)
    

    This returns in terminal:

    worksheet name : Crude Death Rate by Year
    DemographicLineGraph-alias  Year1-value Year1-alias SUM(Population)-alias   ATTR(Ethnicity)-alias   ATTR(Agegroup)-alias    AGG(Deaths)-alias   AGG(Crude death rate)-value AGG(Crude death rate)-alias
    0   Texas   2011    2011    25674681    %many-values%   %many-values%   167,997 654.329454  654.3
    1   Texas   2012    2012    26059203    %many-values%   %many-values%   173,935 667.460935  667.5
    2   Texas   2013    2013    26448193    %many-values%   %many-values%   178,501 674.908112  674.9
    3   Texas   2014    2014    26956959    %many-values%   %many-values%   183,303 679.98397   680.0
    4   Texas   2015    2015    27469114    %many-values%   %many-values%   189,166 688.649805  688.6
    5   Texas   2016    2016    27862596    %many-values%   %many-values%   191,666 687.897136  687.9
    6   Texas   2017    2017    28304596    %many-values%   %many-values%   197,600 698.119839  698.1
    7   Texas   2018    2018    28702243    %many-values%   %many-values%   200,938 700.077691  700.1
    8   Texas   2019    2019    29001602    %many-values%   %many-values%   203,099 700.302694  700.3
    worksheet name : RatesMap
    Geographic Level-alias  Latitude (generated)-value  Latitude (generated)-alias  Longitude (generated)-value Longitude (generated)-alias ATTR(Year)-alias    AGG(Crude Death Rate)-alias AGG(Deaths)-alias
    0   Zavala County   28.864799   28.86   -99.762299  -99.76  2019    850.1   103
    1   Zapata County   26.9454 26.95   -99.1717    -99.17  2019    690.3   98
    2   Young County    33.166801   33.17   -98.688301  -98.69  2019    1,329.5 253
    3   Yoakum County   33.1614 33.16   -102.828003 -102.83 2019    713.6   63
    4   Wood County 32.775501   32.78   -95.397202  -95.40  2019    1,488.3 671
    ... ... ... ... ... ... ... ... ...
    249 Archer County   33.6138 33.61   -98.688301  -98.69  2019    1,072.8 99
    250 Aransas County  28.2209 28.22   -96.875397  -96.88  2019    1,522.6 361
    251 Angelina County 31.276899   31.28   -94.635498  -94.64  2019    997.9   908
    252 Andrews County  32.288502   32.29   -102.637901 -102.64 2019    653.6   126
    253 Anderson County 31.795099   31.80   -95.688698  -95.69  2019    1,130.0 667
    254 rows × 8 columns
    [....]
    

    To get a specific worksheet:

    ws = ts.getWorksheet("Number of Deaths")
    print(ws.data)
    

    EDIT: and to save that specific worksheet to csv:

    [...]
    ws = ts.getWorksheet("Number of Deaths")
    
    print(ws.data)
    ws.data.to_csv('a_creepy_dataframe.csv')
    

    More details on tableauscraper: https://pypi.org/project/TableauScraper/