Using Python to download Tableau Public Data

Hi I am trying to download some data from a public visual : https://public.tableau.com/views/EVv3/Story1 I am trying to reproduce the manual steps (that complete OK),

  1. Go the the URL specified above

  2. click on one of the data items (numbers in the visual) twice - this enables the ability to download the data option

  3. select the download button (bottom right looks like a box with an arrow coming out of it) and select data option

  4. then select full data option and all columns

  5. then select download to as text file url

  6. Data downloads Now when I interrogated the string for the download to text file - I see it has the session number from the original session, but the rest of the URL is static each time. I have tried to capture the session id from the first URL and then effectively trying to create the download URL. It looks correct, but I am getting a 500 error.

I have attached my python code. 2 questions, 1 is this possible on the public website and 2 what am I doing wrong.  I appreciate the security is likely to be much more complex than what I am trying by grabbing the session Id and trying to stick it into a URL, but I am not sure what I should be trying to do.

********************************************** 

import requests
import urllib.request
import json

print(“Running”)

url = ‘https://public.tableau.com/views/EVv3/Story1?:embed=yes&:showVizHome=no&:tabs=no&:toolbar=no/mot-resources/vehicle-fleet-statistics/monthly-electric-and-hybrid-light-vehicle-registrations/nz-light-ev-registration-by-brand-may-2013-june-2019

session = requests.Session()
r = session.get(url)

print(url)

print ("Original Session is : " + str(session))

session_id = (r.headers[‘X-Session-Id’])
print("Session ID is : " + str(session_id))

url2 = ‘https://public.tableau.com/vizql/w/EVv3/v/Story1/vud/sessions/’ + session_id + ‘/views/13720889704328586040_2190967132146547508?csv=true&showall=true’

print(url2)

#filename2 = “C:\temp\metro\elec.csv”
#urllib.request.urlretrieve(url2,filename2)
#r = session.get(url2)
r = requests.get(url2)
print ("New Session is : " + str(session))
print®

Any help much appreciated. Thanks Andrew

I don’t see anything “wrong” with the code. I think tableau.com’s session management is blocking you.

Specifically, the “view id” portion of the download link seems to change for the session as well. And you have no place to grab it from.  I.e. your code shows:

/views/13720889704328586040_2190967132146547508

but when I follow the manual process, I get (for a different session id, of course):

/views/13720889704328586040_8359014638541127868

The “first part” is the same, but the “second part” is not. 

If I use the URL and session ID from my “manual” process, it’s fine. So I think tableau.com is generating a unique link for the session.

Thanks for the help. I have another thought on how to get the session ID. Is there anyway in python you can mimick the browser F12 functionality and specifically record all of the network requests and responses. If I can I was hoping I could capture the network request/responses then search and look for the ID as it is clear to see in the network response via the browser, just not sure how to programmatcially capture this and whether it is possible.

Thanks