tableau.js 库似乎加载了另一个从中获取数据的网址:
从那里开始,它非常类似于这个答案 https://stackoverflow.com/a/62106733/2614364 and this one https://stackoverflow.com/a/61976399/2614364您可以从其中提取 JSON 配置textarea
标签。提取sessionid
构建 URL 来获取数据:
import requests
from bs4 import BeautifulSoup
import json
import re
r = requests.get("https://public.tableau.com/views/COVID-19inMissouri/COVID-19inMissouri",
params = {
":embed": "y",
":showVizHome": "no",
":host_url": "https://public.tableau.com/",
":embed_code_version": 3,
":tabs": "no",
":toolbar": "no",
":animate_transition": "yes",
":display_static_image": "no",
":display_spinner": "no",
":display_overlay": "yes",
":display_count": "yes",
":language": "en",
":loadOrderID": 0
})
soup = BeautifulSoup(r.text, "html.parser")
tableauData = json.loads(soup.find("textarea",{"id": "tsConfigContainer"}).text)
dataUrl = f'https://public.tableau.com{tableauData["vizql_root"]}/bootstrapSession/sessions/{tableauData["sessionid"]}'
r = requests.post(dataUrl, data= {
"sheet_id": tableauData["sheetId"],
})
dataReg = re.search('\d+;({.*})\d+;({.*})', r.text, re.MULTILINE)
info = json.loads(dataReg.group(1))
data = json.loads(dataReg.group(2))
print(data["secondaryInfo"]["presModelMap"]["dataDictionary"]["presModelHolder"]["genDataDictionaryPresModel"]["dataSegments"]["0"]["dataColumns"])
结果不是 JSON,因此需要使用正则表达式对其进行解析,以从中提取 JSON 配置,如上面的代码所示
在 repl.it 上运行这个 https://repl.it/@bertrandmartel/ScrapeTableauMissouri