Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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
Vba 从HTML元素中提取数据_Vba_Excel - Fatal编程技术网

Vba 从HTML元素中提取数据

Vba 从HTML元素中提取数据,vba,excel,Vba,Excel,我需要提取以下HTML标记的d属性: 我尝试使用以下VBA代码,但在强调文本上出现错误: Sub-scraping() 设置ie=CreateObject(“InternetExplorer.Application”) 可见=假 ie.navigate“http:\\website.com” Set elems=ie.Document.getElementsByClassName(“highcharts系列highcharts跟踪器”) Set e=elems.getElementsByTa

我需要提取以下HTML标记的
d
属性:


我尝试使用以下VBA代码,但在强调文本上出现错误:

Sub-scraping()
设置ie=CreateObject(“InternetExplorer.Application”)
可见=假
ie.navigate“http:\\website.com”
Set elems=ie.Document.getElementsByClassName(“highcharts系列highcharts跟踪器”)
Set e=elems.getElementsByTagName(“路径”)
k=1
直到k=e.长度
单元格(k,5)=e.getAttribute(“d”)
k=k+1
环
端接头

我做错了什么?

getElementsByClassName
返回元素集合(可能包含多个、一个或没有成员)

但该集合没有方法
getElementsByTagName
——您需要循环集合中的元素,并对每个元素调用该方法

如果您只希望点击一次,则可以使用:

Set e = elems.getElementsByTagName("path")(0)

你可以用它来获取网站的“目录”

Sub DumpData()

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

URL = "http://finance.yahoo.com/q?s=sbux&ql=1"

'Wait for site to fully load
IE.Navigate2 URL
Do While IE.Busy = True
   DoEvents
Loop

RowCount = 1

With Sheets("Sheet1")
   .Cells.ClearContents
   RowCount = 1
   For Each itm In IE.document.all
      .Range("A" & RowCount) = itm.tagname
      .Range("B" & RowCount) = itm.ID
      .Range("C" & RowCount) = itm.classname
      .Range("D" & RowCount) = Left(itm.innertext, 1024)

      RowCount = RowCount + 1
   Next itm
End With
End Sub

也可以考虑把一个站点的所有内容读入内存,然后解析出你需要的文本。

Sub Test()
    Dim IE As Object

    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .Navigate "http://www.anything.com/" ' should work for any URL
        Do Until .ReadyState = 4: DoEvents: Loop

            x = .document.body.innertext
            x = Replace(x, Chr(10), Chr(13))
            x = Split(x, Chr(13))

            .Quit
        End With

End Sub  
“我在强调文本上出错”-什么错误?什么是强调文本?请在您的帖子中澄清。