Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何使用VBA在Internet Explorer中单击按钮_Excel_Vba_Internet Explorer_Web Scraping - Fatal编程技术网

Excel 如何使用VBA在Internet Explorer中单击按钮

Excel 如何使用VBA在Internet Explorer中单击按钮,excel,vba,internet-explorer,web-scraping,Excel,Vba,Internet Explorer,Web Scraping,我看到了一些例子,解释了如何通过VBA在Internet Explorer中单击按钮。但是,我需要使用的网站不起作用。 *它没有“id”。我看到了querySelector函数,但它不起作用。 地点: 你的选择器错了 html是 <img style="CURSOR:HAND" src="http://www.bmf.com.br/bmfbovespa/images/comum/btoExcel.gif" align="absmiddle" hspace="0" onclick="salv

我看到了一些例子,解释了如何通过VBA在Internet Explorer中单击按钮。但是,我需要使用的网站不起作用。 *它没有“id”。我看到了querySelector函数,但它不起作用。
地点:

你的选择器错了

html是

<img style="CURSOR:HAND" src="http://www.bmf.com.br/bmfbovespa/images/comum/btoExcel.gif" align="absmiddle" hspace="0" onclick="salvaxls()">
您还可以将$ends与运算符一起使用,并以src为目标

[src$='btoExcel.gif']

使用正确的页面加载等待,如下所示

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub RetrieveInfo()
    Dim ie As InternetExplorer
    Set ie = New InternetExplorer

    With ie
        .Visible = True
        .Navigate2 "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"

        While .Busy Or .readyState < 4: DoEvents: Wend

        .document.querySelector("[src$='btoExcel.gif']").Click

        Stop

    End With
End Sub
选项显式
'VBE>工具>参考:
“Microsoft Internet控件
公共子检索信息()
Dim ie作为InternetExplorer
Set ie=新的InternetExplorer
与ie
.Visible=True
.导航2“http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"
当.Busy或.readyState<4:DoEvents:Wend时
.document.querySelector(“[src$='btoExcel.gif']”)。单击
停止
以
端接头


关于如何与“保存/打开”对话框交互,有很多问题。就个人而言,我更喜欢使用selenium basic实现自动化,并使用Chrome来完全避免这个问题

我正在开展一项个人活动,鼓励人们在可能的情况下使用HTTP请求,因此我的两分钱是:

Sub Taxas()
Dim req As New WinHttpRequest
Dim doc As New HTMLDocument
Dim table As HTMLTable
Dim tableRow As HTMLTableRow
Dim reqURL As String
Dim mainURL As String
Dim dateOfInterest As Date
Dim param1 As String
Dim param2 As String
Dim param3 As String
Dim i As Long
dateOfInterest = Date - 1 '11/04/2019 use whichever date you want
param1 = Format(dateOfInterest, "dd/mm/yyyy")
param2 = Format(dateOfInterest, "yyyymmdd")
param3 = "PRE" 'this can be changed according to which element from the drop down list on the top left you need
mainURL = "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"
reqURL = mainURL & "?Data=" & param1 & "&Data1=" & param2 & "&slcTaxa=" & param3


With req
    .Open "POST", reqURL, False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .send
    doc.body.innerHTML = .responseText
End With
Set table = doc.getElementById("tb_principal1")
i = 1
For Each tableRow In table.Rows
    If tableRow.Cells(0).className <> "tabelaTitulo" And tableRow.Cells(0).className <> "tabelaItem" Then
        ThisWorkbook.Worksheets(1).Cells(i, "A") = CDbl(Replace((tableRow.Cells(0).innerText), ",", "."))
        ThisWorkbook.Worksheets(1).Cells(i, "B") = CDbl(Replace((tableRow.Cells(1).innerText), ",", "."))
        ThisWorkbook.Worksheets(1).Cells(i, "C") = CDbl(Replace((tableRow.Cells(2).innerText), ",", "."))
        i = i + 1
    End If
Next tableRow

End Sub
亚类群()
Dim req作为新的WinHttpRequest
Dim doc作为新的HTMLDocument
将表格调暗为HTMLTable
将tableRow设置为HTMLTableRow
将URL设置为字符串
将mainURL设置为字符串
Dim dateOfInterest作为日期
Dim param1作为字符串
Dim param2作为字符串
Dim param3作为字符串
我想我会坚持多久
dateOfInterest=日期-2019年4月1日使用您想要的日期
参数1=格式(感兴趣的日期,“dd/mm/yyyy”)
参数2=格式(感兴趣的日期,“yyyymmdd”)
param3=“PRE”'这可以根据您需要的左上角下拉列表中的哪个元素进行更改
mainURL=”http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"
reqURL=mainURL&“?Data=“¶m1&”&Data1=“¶m2&”&slcTaxa=“¶m3
带req
.打开“POST”,请求URL,错误
.setRequestHeader“内容类型”、“应用程序/x-www-form-urlencoded”
.发送
doc.body.innerHTML=.responseText
以
Set table=doc.getElementById(“tb_principal1”)
i=1
对于table.Rows中的每个tableRow
如果tableRow.Cells(0).className“tabelaTitulo”和tableRow.Cells(0).className“tabelaItem”,则
ThisWorkbook.Worksheets(1).单元格(i,“A”)=CDbl(替换((tableRow.Cells(0).innerText),“,”,”)
ThisWorkbook.Worksheets(1).Cells(i,“B”)=CDbl(替换((tableRow.Cells(1).innerText),“,”,”)
ThisWorkbook.Worksheets(1).单元格(i,“C”)=CDbl(替换((tableRow.Cells(2).innerText),“,”,”)
i=i+1
如果结束
下一张桌子行
端接头
确保转到VB编辑器>工具>参考,并添加
Microsoft WinHTTP服务5.1版
Microsoft HTML对象库

使用此方法,您无需下载excel文件。您可以从源代码中获得正确的数据,并将其写入工作表中

学习代码,尝试从中学习,我保证它将使您在未来的任何网页抓取项目中的生活更加轻松


干杯

上述按钮位于页面顶部和右侧的中。姓名:“exportar para o excel”@Vinicius,你的问题解决了吗?我测试了QHarr建议的解决方案,看起来他的代码运行良好,可以解决您的问题。我建议您测试他的代码,并将他的建议标记为该线程的可接受答案。它可以在将来帮助其他社区成员解决类似的问题。如果您有任何进一步的问题,请告诉我们。谢谢你的理解+1个用于QHarr
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub RetrieveInfo()
    Dim ie As InternetExplorer
    Set ie = New InternetExplorer

    With ie
        .Visible = True
        .Navigate2 "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"

        While .Busy Or .readyState < 4: DoEvents: Wend

        .document.querySelector("[src$='btoExcel.gif']").Click

        Stop

    End With
End Sub
Sub Taxas()
Dim req As New WinHttpRequest
Dim doc As New HTMLDocument
Dim table As HTMLTable
Dim tableRow As HTMLTableRow
Dim reqURL As String
Dim mainURL As String
Dim dateOfInterest As Date
Dim param1 As String
Dim param2 As String
Dim param3 As String
Dim i As Long
dateOfInterest = Date - 1 '11/04/2019 use whichever date you want
param1 = Format(dateOfInterest, "dd/mm/yyyy")
param2 = Format(dateOfInterest, "yyyymmdd")
param3 = "PRE" 'this can be changed according to which element from the drop down list on the top left you need
mainURL = "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"
reqURL = mainURL & "?Data=" & param1 & "&Data1=" & param2 & "&slcTaxa=" & param3


With req
    .Open "POST", reqURL, False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .send
    doc.body.innerHTML = .responseText
End With
Set table = doc.getElementById("tb_principal1")
i = 1
For Each tableRow In table.Rows
    If tableRow.Cells(0).className <> "tabelaTitulo" And tableRow.Cells(0).className <> "tabelaItem" Then
        ThisWorkbook.Worksheets(1).Cells(i, "A") = CDbl(Replace((tableRow.Cells(0).innerText), ",", "."))
        ThisWorkbook.Worksheets(1).Cells(i, "B") = CDbl(Replace((tableRow.Cells(1).innerText), ",", "."))
        ThisWorkbook.Worksheets(1).Cells(i, "C") = CDbl(Replace((tableRow.Cells(2).innerText), ",", "."))
        i = i + 1
    End If
Next tableRow

End Sub