Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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从URL获取数据_Excel_Vba - Fatal编程技术网

Excel 使用VBA从URL获取数据

Excel 使用VBA从URL获取数据,excel,vba,Excel,Vba,我使用下面的代码从网站上获取数据 For Each link In doc.Links If link.innerText = "fener" Then link.Click Exit For End If Next link While ie.Busy Or ie.readyState < 4: DoEvents: Wend Sheets("Final").Range("E5") = ie.document.g

我使用下面的代码从网站上获取数据

For Each link In doc.Links
If link.innerText = "fener" Then
link.Click
Exit For
End If
Next link
        While ie.Busy Or ie.readyState < 4: DoEvents: Wend

Sheets("Final").Range("E5") = ie.document.getElementById("InnerContent").getElementsByClassName("Template")(0).getElementsByClassName("View")(0).getElementsByClassName("Main")(8).getElementsByClassName("Field")(0).innerText

Sheets("Mail").Range("A16") = ie.document.getElementById("InnerContent").getElementsByClassName("Template")(0).getElementsByClassName("View")(0).getElementsByClassName("Main")(9).getElementsByClassName("Field")(0).innerText
Sheets("Sheet1").Range("E5") = ie.document.getElementById("InnerContent").getElementsByClassName("Template")(0).getElementsByClassName("View")(0).getElementsByClassName("Main")(8).getElementsByClassName("Field")(0).innerText

如果某些元素的classname
Main
后跟classname
Field
的元素不感兴趣,则需要执行循环并测试ANC、EDC的节点的
.innerText
,请参阅

Public Sub Demo()

    Dim html As MSHTML.HTMLDocument

    Set html = New MSHTML.HTMLDocument

    html.body.innerHTML = [A3]

    Dim nodes As Object, node As Object, i As Long

    Set nodes = html.querySelectorAll(".Main > .Literal[nowrap]")

    For i = 0 To nodes.Length - 1
        Set node = nodes.Item(i)
        Select Case node.innerText
        Case "ANC:", "EDC:", "SEE:"
            Debug.Print node.NextSibling.innerText
        End Select
    Next i

End Sub
如果
.Main>.Literal[nowrap]
的css选择器(它为class
Main
且具有class
Literal
nowrap
属性的元素的子节点进行选择)具有足够的选择性,则代码变得更简单:

Public Sub Demo()

    Dim html As MSHTML.HTMLDocument

    Set html = New MSHTML.HTMLDocument

    html.body.innerHTML = [A3]

    Dim nodes As Object, node As Object, i As Long

    Set nodes = html.querySelectorAll(".Main > .Literal[nowrap]")

    For i = 0 To nodes.Length - 1
        Debug.Print nodes(i).NextSibling.innerText
    Next i

End Sub

注意:我正在从Activesheet的A3单元格中读取您的HTML

需要:VBE>工具>引用>对Microsoft HTML对象库的引用


阅读css选择器:

您希望检索什么?你所说的病例数可以改变是什么意思?如果可能存在一些不感兴趣的Main后跟字段,那么您将需要执行一个循环并测试节点的.innerText,其中包含ANC、EDC的类名称字符串,请参见仍然没有任何效果。。。仍然需要帮助谢谢你的回复。我可以连接一个网站,然后从那里获取价值。实际上,在我的代码中,从网站复制值,然后粘贴到ActiveSheet E5。此外,我还编辑了我的代码以增加理解。是否只有三个值?如果上面的答案遗漏了这一点,那么在工作表上加几行就很容易了。您是否尝试实现我的代码并检查它是否为ie.document.getElementsByCassName(“Main”)中的每个表返回了正确的值?Set Table=ie.document.getElementsByCassName(“Main”)Set tRows=Table(0)。getElementsByTagName(“tr”)Set tHead=Table(0)。getElementsByTagName(“td”)为AD Sheet21.单元格(rNum,cNum).Value=h.innerText cNum=cNum+1 Next rNum=rNum+1 cNum=1 tRows中的每个r设置tCells=r.getElementsByTagName(“td”)tCells Sheet21中的每个c。Cells(rNum,cNum)。Value=c.innerText cNum=cNum+1 Next'发现了问题,在该页中还有另一个主表。所以,当我试图在第一个主表中获取数据时,它的长度正在改变。第二个主表有20条主线。所以我尝试将整个表复制到excel。代码如下。不过我试过了。代码只获取第一个主表,而不获取第二个主表。所以我真的结巴了。我也试过你的代码。事实上它什么都不做。有没有网址可以测试?该代码适用于您提供的html。它将结果写入即时窗口(Ctrl+G)
Public Sub Demo()

    Dim html As MSHTML.HTMLDocument

    Set html = New MSHTML.HTMLDocument

    html.body.innerHTML = [A3]

    Dim nodes As Object, node As Object, i As Long

    Set nodes = html.querySelectorAll(".Main > .Literal[nowrap]")

    For i = 0 To nodes.Length - 1
        Set node = nodes.Item(i)
        Select Case node.innerText
        Case "ANC:", "EDC:", "SEE:"
            Debug.Print node.NextSibling.innerText
        End Select
    Next i

End Sub
Public Sub Demo()

    Dim html As MSHTML.HTMLDocument

    Set html = New MSHTML.HTMLDocument

    html.body.innerHTML = [A3]

    Dim nodes As Object, node As Object, i As Long

    Set nodes = html.querySelectorAll(".Main > .Literal[nowrap]")

    For i = 0 To nodes.Length - 1
        Debug.Print nodes(i).NextSibling.innerText
    Next i

End Sub