Skip to content

Instantly share code, notes, and snippets.

@jheasly
Last active March 9, 2021 01:01
Show Gist options
  • Save jheasly/2d03b0a360249618739e6040e1a771ca to your computer and use it in GitHub Desktop.
Save jheasly/2d03b0a360249618739e6040e1a771ca to your computer and use it in GitHub Desktop.
An example of from using Python to get the API behind a Tableau site, rather than scraping the HTML. Per the author, Jeremy J. Bowers, it will work for any Tableau vizql implementation, with a bit of URL substitution.
# Related, from News Nerdery thread:
#
# aricchokey 2 hours ago
# @ejmurra Looks like another way to grab the data is by tacking on a ".csv" at the end of the chosen sheet. It will
# return the delimited version of the data/trigger the download from Tableau, too.
# Like https://bi.ahca.myflorida.com/t/ABICC/views/Public/HospitalBedsHospital.csv or
# https://bi.ahca.myflorida.com/t/ABICC/views/Public/ICUBedsCounty.csv. Might get rid of the need for a payload
# in your script.
from bs4 import BeautifulSoup
import json
import requests
headers = {
'sec-fetch-mode': 'cors',
'origin': 'https://bi.ahca.myflorida.com',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'cookie': 'tableau_locale=en',
'x-tsi-active-tab': 'Hospital%20BedsCounty',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
'accept': 'text/javascript',
'referer': 'https://bi.ahca.myflorida.com/t/ABICC/views/Public/HospitalBedsCounty?%3AshowAppBanner=false&%3Adisplay_count=n&%3AshowVizHome=n&%3Aorigin=viz_share_link&%3AisGuestRedirectFromVizportal=y&%3Aembed=y',
'authority': 'bi.ahca.myflorida.com',
'sec-fetch-site': 'same-origin',
'dnt': '1',
}
data = {
'worksheetPortSize': '{"w":1680,"h":536}',
'dashboardPortSize': '{"w":1680,"h":536}',
'clientDimension': '{"w":1680,"h":220}',
'renderMapsClientSide': 'true',
'isBrowserRendering': 'true',
'browserRenderingThreshold': '100',
'formatDataValueLocally': 'false',
'clientNum': '',
'navType': 'Reload',
'navSrc': 'Top',
'devicePixelRatio': '2',
'clientRenderPixelLimit': '25000000',
'allowAutogenWorksheetPhoneLayouts': 'true',
'sheet_id': 'Hospital%20BedsCounty',
'showParams': '{"checkpoint":false,"refresh":false,"refreshUnmodified":false}',
'stickySessionKey': '{"featureFlags":"{}","isAuthoring":false,"isOfflineMode":false,"lastUpdatedAt":1585873049792,"workbookId":1770}',
'filterTileSize': '200',
'locale': 'en_US',
'language': 'en',
'verboseMode': 'false',
':session_feature_flags': '{}',
'keychain_version': '1'
}
baseurl = "https://bi.ahca.myflorida.com/t/ABICC/views/Public/HospitalBedsCounty?%3AshowAppBanner=false&%3Adisplay_count=n&%3AshowVizHome=n&%3Aorigin=viz_share_link&%3AisGuestRedirectFromVizportal=y&%3Aembed=y"
r = requests.get(baseurl, headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
config = json.loads(soup.select('#tsConfigContainer')[0].text)
sessionid = config['sessionid']
response = requests.post(f'https://bi.ahca.myflorida.com/vizql/t/ABICC/w/Public/v/HospitalBedsCounty/bootstrapSession/sessions/{sessionid}', headers=headers, data=data)
@jheasly
Copy link
Author

jheasly commented Mar 9, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment