Home > front end >  Scraping a public tableau dashboard with custom tables
Scraping a public tableau dashboard with custom tables

Time:10-09

This is a follow-up question to enter image description here

CodePudding user response:

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?:embed=y&showTabs=true&:display_count=n&:showVizHome=n&:origin=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/

  • Related