【问题标题】:Power BI - Get data from post requestPower BI - 从发布请求中获取数据
【发布时间】:2019-08-15 15:12:36
【问题描述】:

我正在尝试将 .cvs 文件从 https://www2.cetip.com.br/TitulosCRI 自动获取到 Power BI。要手动下载文件,您必须先点击“Enviar”按钮(发送,葡萄牙语),然后点击“Exportar para CSV”按钮(导出为 CSV)。

我的第一次尝试是找出文件的下载链接,但结果却是网页链接本身。

阅读后,我了解到当我单击按钮时,我可能会发出 HTTP 发布请求。我试图找到一些 Power BI 代码示例来提出此类请求,但由于我缺乏该主题的知识,因此很难理解所提供的代码。

通过分析页面的源代码,发现如下代码,可能与请求有关:

<input type="submit" name="btExportarCSV" value="Exportar para CSV" id="btExportarCSV" class="button">

谁能帮助我了解如何使用 Power BI 自动获取此文件?

【问题讨论】:

  • 我不认为这是您可以直接在 Power BI 中执行的操作。您也许可以在 MSFlow 中执行此操作,然后将文件拖放/导入 Power BI 可以读取的位置。

标签: javascript json httprequest powerbi


【解决方案1】:

Lucca,我相信你来自巴西,但无论如何我会用英文写,这样人们才能理解答案,好吗?

我多年来一直在尝试抓取这个网页(使用 R、python 和 excel+vba),最后我找到了解决方案:herehere

我在 Excel + VBA 中找到了解决方案。我在下面发布:

Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

'------------------------------------------------------

Public Sub AddReference()
    ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\SysWOW64\UIAutomationCore.dll"
End Sub

'-------------------------------------------------------------'
Sub baixa_titulos_CRI()
'Dimensioning the vars
Dim IE As Object
Dim county As String
Dim htmlDoc As Object
Dim sURL As String
Dim buttonclick As Object
Dim ShowMore As Object
Dim exportar_para_CSV As Object
'----------------'
Set IE = CreateObject("internetexplorer.application")
sURL = "https://www2.cetip.com.br/TitulosCRI"
With IE
.Navigate (sURL)
.Visible = True
End With
'I put some waits to run the webpage properly.
WaitIE2 IE, 2000
Set htmlDoc = IE.Document
WaitIE2 IE, 1000
'here it will press the button "enviar"
Set button_send = htmlDoc.getElementById("btEnviar")
button_send.Click
WaitIE2 IE, 2000
'here it will press the button "exportar para CSV"
Set exportar_para_CSV = htmlDoc.getElementById("btExportarCSV")
exportar_para_CSV.Click
WaitIE2 IE, 2000
'this is the solution that I found to press the button "Salvar" in IE.
'I also save the directory that I wanted as default so IE save in the path properly.
'I still couldnt find a solution to name of the file and to the path to save.
'The solution I adapted is to list.files and the date it was saved to compare with older files (as I think you'll do it also I mentioned it here)
Dim o As IUIAutomation
Dim e As IUIAutomationElement
Set o = New CUIAutomation
Dim h As Long
h = IE.Hwnd
If h = 0 Then Exit Sub
Set e = o.ElementFromHandle(ByVal h)
Dim iCnd As IUIAutomationCondition
Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
Dim Button As IUIAutomationElement
Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
WaitIE2 IE, 2000
IE.Quit
Set IE = Nothing
End Sub
'-----------------------------------------------------------
Sub WaitIE2(IE As Object, Optional time As Long = 250)
Dim i As Long
Do
    Sleep time
    Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.ReadyState = 4) & _
                vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
    i = i + 1
Loop Until IE.ReadyState = 4 Or Not IE.Busy
End Sub

希望对你有帮助

最好的,费利佩·里贝罗

[excel] [vba]

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 1970-01-01
    • 2021-09-22
    • 2019-02-04
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多