【问题标题】:Scraping data from Tableau从 Tableau 中抓取数据
【发布时间】:2020-04-25 05:50:08
【问题描述】:

我必须将数据从表格工作簿抓取到 csv 文件。 https://public.tableau.com/views/2020_04_06_COVID19_India/Dashboard_India_Cases?:embed=y&:showVizHome=no&:host_url=https%3A%2F%2Fpublic.tableau.com%2F&:embed_code_version=3&:tabs=no&:toolbar=yes&:animate_transition=yes&:display_static_image=no&:display_spinner=no&:display_overlay=yes&:display_count=yes&publish=yes&:loadOrderID=0

我尝试了以下方法,但没有得到任何输出。

main.py

import requests
from bs4 import BeautifulSoup


 r = requests.get("https://public.tableau.com/views/2020_04_06_COVID19_India/Dashboard_India_Cases?:embed=y&:showVizHome=no&:host_url=https%3A%2F%2Fpublic.tableau.com%2F&:embed_code_version=3&:tabs=no&:toolbar=yes&:animate_transition=yes&:display_static_image=no&:display_spinner=no&:display_overlay=yes&:display_count=yes&publish=yes&:loadOrderID=0")

     soup = BeautifulSoup(r.content, "html.parser")

     for td in soup.findAll("table"):

     for a in td.findAll("tr"):
      print(a.find('td'))

【问题讨论】:

  • 我在此页面中没有看到任何 <table> 元素
  • 但是如何从工作簿中提取数据,例如我为普通站点编写的代码。
  • 数据似乎在canvas 中。您应该搜索一下如何使用 Python 从canvas 检索数据。有类似的主题,如this 一个或者这个one

标签: python web-scraping beautifulsoup


【解决方案1】:

我制作了this python tableau scraper library,它列出了工作表并将数据导出到每个工作表的 pandas 数据框中。例如,以下获取您要查找的表:

from tableauscraper import TableauScraper as TS

url = "https://public.tableau.com/views/2020_04_06_COVID19_India/Dashboard_India_Cases"

ts = TS()
ts.loads(url)
dashboard = ts.getDashboard()

for t in dashboard.worksheets:
    #show worksheet name
    print(f"WORKSHEET NAME : {t.name}")
    #show dataframe for this worksheet
    print(t.data)

run this code on repl.it

【讨论】:

  • 感谢图书馆!我只是在几个随机页面上运行它 - 完美无缺!
  • 这是否也适用于 Tableau Server(非公共)仪表板?我尝试在我们发布到本地 Tableau 服务器的一些仪表板上使用它,并从 TableauScraper.py 的第 80 行不断收到以下错误“AttributeError:'NoneType' object has no attribute 'text'”。它源于对 "soup.find("textarea", {"id": "tsConfigContainer"}).text 的调用
  • 有没有使用tableauserverclient库登录到Tableau server(非公开)然后使用TableauScraper获取数据的例子? @bertrand-martel
  • @vmachan 它在经过身份验证的 tableau 服务器上不起作用,我从来没有在经过身份验证的 tableau 链接上工作,但是,如果身份验证通过 cookie 标头,您可以使用 ts.session = requests.Session() 在开始,像这样stackoverflow.com/questions/17224054/…。不确定它是否会起作用,但如果有额外的 CSRF 机制,比如this
猜你喜欢
  • 2020-11-11
  • 2021-01-15
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多