Excel 导航到具有Web刮片功能的表体

Excel 导航到具有Web刮片功能的表体,excel,vba,web-scraping,Excel,Vba,Web Scraping,我试图从一个网站获取数据到我的excel工作表,但不知何故,无论我做什么,我都无法导航到表体。请查看网站和下面的代码,并告诉我如何获取最新的1Y、2Y、,我的excel表格中有10Y。代码如下: Option Explicit Sub updatePKRV() Dim ieobj As InternetExplorer Dim iedoc As HTMLDocument Dim htmlele As IHTMLElement Dim HTMLRow As IHTMLElementCollec

我试图从一个网站获取数据到我的excel工作表,但不知何故,无论我做什么,我都无法导航到表体。请查看网站和下面的代码,并告诉我如何获取最新的1Y、2Y、,我的excel表格中有10Y。代码如下:

Option Explicit

Sub updatePKRV()

Dim ieobj As InternetExplorer
Dim iedoc As HTMLDocument
Dim htmlele As IHTMLElement
Dim HTMLRow As IHTMLElementCollection
Dim HTMLIT As IHTMLElement
Dim ws As Worksheet

Set ieobj = New InternetExplorer
ieobj.Visible = False
ieobj.navigate "https://fma.com.pk/index.php/pkrv/"

Do While ieobj.Busy = True Or ieobj.readyState <> READYSTATE_COMPLETE
Application.Wait Now + TimeValue("00:00:01")
Loop

Set iedoc = ieobj.document
Set htmlele = iedoc.getElementById("table_2")
'Set HTMLRow = htmlele.getElementsByTagName("tr")

Debug.Print htmlele.Children(0).textContent

End Sub
选项显式
子更新pkrv()
Dim ieobj作为InternetExplorer
Dim iedoc作为HTMLDocument
将htmlele设置为iHtmlement
调暗HTMLRow作为IHTMlementCollection
如IHTMLElement所示变暗
将ws设置为工作表
Set ieobj=新的InternetExplorer
ieobj.Visible=False
“导航”https://fma.com.pk/index.php/pkrv/"
当ieobj.Busy=True或ieobj.readyState readyState\u完成时执行
应用程序。立即等待+时间值(“00:00:01”)
环
设置iedoc=ieobj.document
设置htmlele=iedoc.getElementById(“表2”)
'Set HTMLRow=htmlele.getElementsByTagName(“tr”)
Debug.Print htmlele.Children(0).textContent
端接头

变动后

Option Explicit

Sub updatePKRV()

Dim ieobj As InternetExplorer
Dim iedoc As HTMLDocument
Dim htmlele As IHTMLElement
Dim HTMLRow As IHTMLElementCollection
Dim HTMLIT As IHTMLElement
Dim nodeList As Object, i As Long, arr()

Dim ws As Worksheet

Set ieobj = New InternetExplorer
ieobj.Visible = False
ieobj.navigate "https://fma.com.pk/index.php/pkrv/"

Do While ieobj.Busy = True Or ieobj.readyState <> READYSTATE_COMPLETE
    Application.Wait Now + TimeValue("00:00:01")
Loop

Set iedoc = ieobj.document
Set htmlele = iedoc.getElementById("table_2")

Set nodeList = ieobj.document.querySelectorAll("#table_2 tr:nth-of-type(2) .column-date,  #table_2 tr:nth-of-type(2) [class*=y]")

ReDim arr(1 To 11)
For i = 0 To 10
    arr(i + 1) = nodeList.Item(i).innerText ''This is where is gets an error
Next

ActiveSheet.Cells(2, 1).Resize(1, UBound(arr, 2)) = arr

End Sub
选项显式
子更新pkrv()
Dim ieobj作为InternetExplorer
Dim iedoc作为HTMLDocument
将htmlele设置为iHtmlement
调暗HTMLRow作为IHTMlementCollection
如IHTMLElement所示变暗
Dim nodeList作为对象,i作为Long,arr()
将ws设置为工作表
Set ieobj=新的InternetExplorer
ieobj.Visible=False
“导航”https://fma.com.pk/index.php/pkrv/"
当ieobj.Busy=True或ieobj.readyState readyState\u完成时执行
应用程序。立即等待+时间值(“00:00:01”)
环
设置iedoc=ieobj.document
设置htmlele=iedoc.getElementById(“表2”)
Set nodeList=ieobj.document.queryselectoral(“#table_2 tr:n类型(2).列日期,#table_2 tr:n类型(2)[class*=y]”)
ReDim arr(1至11)
对于i=0到10
arr(i+1)=节点列表项(i).innerText''这是is获取错误的地方
下一个
单元格(2,1)。调整大小(1,UBound(arr,2))=arr
端接头
您需要“标题”第一个日期列,然后是第二行中的前10个年份列。您可以使用css选择器来实现这一点

#table_2 tr:nth-of-type(2) .column-date,  #table_2 tr:nth-of-type(2) [class*=y]
这将检索第二行的节点列表

tr:nth-of-type(2) 
在id为
表2

#table_2 
将子项与类
列日期匹配

.column-date
或(

包含(
*
)字母y(表示年份)的类

注:

  • 我正在匹配当前多值类的单个类

  • 页面加载缓慢,因此您可能需要一个定时循环来等待元素完全加载

  • 使用该节点列表,您希望从0到10,以获得第一个日期字段和10个第一年

    Dim nodeList As Object, i As Long, arr()
    
    Set nodeList = ie.document.querySelectorAll("#table_2 tr:nth-of-type(2) .column-date,  #table_2 tr:nth-of-type(2) [class*=y]")
    
    ReDim arr(1 To 11)
    For i = 0 To 10
        arr(i+1) = nodeList.item(i).innerText   
    Next
    
    ActiveSheet.Cells(2,1).Resize(1, UBound(arr, 1)) = arr
    

    请在此处阅读有关css选择器的信息:


    匹配和输出的快速浏览器测试:



    2021-03-05更新

    #table_2 tbody tr:nth-of-type(1) .column-date,  #table_2  tbody tr:nth-of-type(1) [class*=y]
    

    要获取表体中的第一行(即排除标题行并获取最新日期)

    您好,谢谢您的回复。我更改了代码,使其看起来像这样,但它给出了一个运行时错误'424'所需的对象。请再看一遍这个问题。我已经用你的代码编辑过了。非常感谢。旧习惯<代码>活动页。单元格(2,1)。重新调整大小(1,UBound(arr,1))=arr
    hi。这次效果非常好。但是现在,由于网站已经更新,它应该在第二行中获取可用的最新数据,但它不是。
    \table\u 2 tbody tr:nth类型(1)。column date,\table\u 2 tbody tr:nth类型(1)[class*=y]
    #table_2 tbody tr:nth-of-type(1) .column-date,  #table_2  tbody tr:nth-of-type(1) [class*=y]