[英]Copying a specific word of webpage to excel using vbs
我想将网页中出现的特定文本“ 110001”复制到Excel文件中的工作表中。 请告诉我如何通过vbscript做到这一点。 以下是我要复制的相关源代码行。
<h1 id="h1">110001</h1>
下面是我正在使用的代码。 此代码中的问题是它没有从网页复制任何内容。 另外,它在最后三行给出运行时错误1004-Range(“ A1”)。Select
Private Sub blahblah_Click()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://someWebsite"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set y = ie.Document.getelementbyid("h1")
ie.ExecWB 12, 2
Workbooks("Book1.xlsm").Activate
Sheets("Sheet2").Select
Range("A1").Select
ActiveSheet.Paste
End Sub
y = ie.document.getElementById(“ ad-space.holdings”)。innerHTML
上面的代码向我显示错误或err.number = 424。
让我知道我需要从VBA引用中选择任何内容吗?
可以避免粘贴粘贴吗? 试试这个吧。
Private Sub blahblah_Click()
Dim ie As Object
Dim WB As Workbook
Dim WS As Worksheet
Set WB = Workbooks("Book1.xlsm")
Set WS = WB.Sheets("Sheet2")
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://someWebsite"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
y = ie.Document.getelementbyid("h1").innerHTML
WS.Range("A1").Value = y
End Sub
基本上,您需要innerHTML
部分,因为这是获取具有该元素ID的HTML标记内的值所需的控件。
Private Sub blahblah_Click()
Dim ie As Object
Dim WB As Workbook
Dim WS As Worksheet
Set WB = Workbooks.Add
Set WS = WB.Sheets("Sheet2")
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://etfdb.com/etf/EEM/#holdings"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
y = ie.document.getElementById("ad-space.holdings").innerHTML
WS.Range("A1").Value = y
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.