也许您可以模拟页面正在执行的加载数据的AJAX请求.下面是一个例子:
import pandas as pd
import requests
RT = []
api_url = "https://wabi-brazil-south-api.analysis.windows.net/public/reports/querydata?synchronous=true"
def get_query(RT):
query = {
"cancelQueries": [],
"modelId": 4824238,
"queries": [
{
"Query": {
"Commands": [
{
"SemanticQueryDataShapeCommand": {
"Binding": {
"DataReduction": {
"DataVolume": 3,
"Primary": {
"Window": {
"Count": 500,
"RestartTokens": RT,
}
},
},
"Primary": {
"Groupings": [
{"Projections": [0, 1, 2], "Subtotal": 1}
]
},
"Version": 1,
},
"ExecutionMetricsKind": 1,
"Query": {
"From": [
{
"Entity": "RESULTADOS - DENATRAN 2 0",
"Name": "r",
"Type": 0,
},
{
"Entity": "Tabela Mestre VE",
"Name": "t",
"Type": 0,
},
],
"OrderBy": [
{
"Direction": 1,
"Expression": {
"Column": {
"Expression": {
"SourceRef": {"Source": "r"}
},
"Property": "CIDADE",
}
},
}
],
"Select": [
{
"Column": {
"Expression": {
"SourceRef": {"Source": "r"}
},
"Property": "CIDADE",
},
"Name": "RESULTADOS - DENATRAN 2 0.CIDADE",
},
{
"Aggregation": {
"Expression": {
"Column": {
"Expression": {
"SourceRef": {"Source": "r"}
},
"Property": "QTD",
}
},
"Function": 0,
},
"Name": "Sum(RESULTADOS - DENATRAN 2 0.QTD)",
},
{
"Arithmetic": {
"Left": {
"Aggregation": {
"Expression": {
"Column": {
"Expression": {
"SourceRef": {
"Source": "r"
}
},
"Property": "QTD",
}
},
"Function": 0,
}
},
"Operator": 3,
"Right": {
"ScopedEval": {
"Expression": {
"Aggregation": {
"Expression": {
"Column": {
"Expression": {
"SourceRef": {
"Source": "r"
}
},
"Property": "QTD",
}
},
"Function": 0,
}
},
"Scope": [],
}
},
},
"Name": "Divide(Sum(RESULTADOS - DENATRAN 2 0.QTD), ScopedEval(Sum(RESULTADOS - DENATRAN 2 0.QTD), []))",
},
],
"Version": 2,
"Where": [
{
"Condition": {
"In": {
"Expressions": [
{
"Column": {
"Expression": {
"SourceRef": {
"Source": "r"
}
},
"Property": "DT FROTA",
}
}
],
"Values": [
[
{
"Literal": {
"Value": "datetime'2023-07-01T00:00:00'"
}
}
]
],
}
}
},
{
"Condition": {
"In": {
"Expressions": [
{
"Column": {
"Expression": {
"SourceRef": {
"Source": "t"
}
},
"Property": "TIPO VEICULO",
}
}
],
"Values": [
[
{
"Literal": {
"Value": "'CARROS'"
}
}
]
],
}
}
},
],
},
}
}
]
},
"QueryId": "",
}
],
"version": "1.0.0",
}
return query
headers = {"X-PowerBI-ResourceKey": "552598be-7e87-47c3-ae3e-a65dcaa097cf"}
def find_key_recursively(obj, key):
if isinstance(obj, dict):
if key in obj:
yield obj[key]
for v in obj.values():
yield from find_key_recursively(v, key)
elif isinstance(obj, list):
for v in obj:
yield from find_key_recursively(v, key)
all_data = []
while True:
data = requests.post(api_url, json=get_query(RT), headers=headers).json()
x = next(find_key_recursively(data, "DM1"))
for d in x:
if len(d["C"]) == 1:
all_data.append((d["C"][0], 1))
else:
all_data.append((d["C"][0], d["C"][1]))
try:
RT = next(find_key_recursively(data, "RT"))
except StopIteration:
break
print(RT)
df = pd.DataFrame(all_data, columns=["Name", "Count"])
print(df)
打印:
...
3112 VOLTA REDONDA 217
3113 VOTORANTIM 78
3114 VOTUPORANGA 51
3115 WAGNER 1
3116 WENCESLAU BRAZ 2
3117 WENCESLAU GUIMARAES 1
3118 WESTFALIA 4
3119 WITMARSUM 1
3120 XANGRI-LA 46
3121 XANXERE 34
3122 XAPURI 1
3123 XAXIM 18
3124 XINGUARA 3
3125 XIQUE-XIQUE 6
3126 ZABELE 1
3127 ZACARIAS 2
3128 ZE DOCA 4