Excel VBA从多个网站抓取数据
我正在尝试使用VBA从Investment.com的多个页面中提取商品/股票价格,并将其插入excel电子表格 以下代码是我为单一价格所做的工作,在本例中为gold:Excel VBA从多个网站抓取数据,excel,vba,Excel,Vba,我正在尝试使用VBA从Investment.com的多个页面中提取商品/股票价格,并将其插入excel电子表格 以下代码是我为单一价格所做的工作,在本例中为gold: Sub Extractdatafromwebsite() Dim ie As New InternetExplorer Dim doc As HTMLDocument ie.Visible = False ie.navigate "http://uk.investing.com/commodities/gold" Do
Sub Extractdatafromwebsite()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Visible = False
ie.navigate "http://uk.investing.com/commodities/gold"
Do
DoEvents
Loop Until ie.READYSTATE = READYSTATE_COMPLETE
Set doc = ie.document
output = doc.GetElementById("last_last").innerText
Range("A1").Value = output
ie.Quit
End Sub
然而,我需要从多个网站的数据,以获得不同的价格,所有在同一时间
我试图详细说明我的代码,下面的示例是我试图显示黄金和白银价格的示例,但它仅在单元格A1和A2中显示黄金价格:
Sub Extractdatafromwebsite()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Visible = False
ie.navigate "http://uk.investing.com/commodities/gold"
Do
DoEvents
Loop Until ie.READYSTATE = READYSTATE_COMPLETE
Set doc = ie.document
output = doc.GetElementById("last_last").innerText
Range("A1").Value = output
ie.Quit
ie.navigate "http://uk.investing.com/commodities/silver"
Set doc = ie.document
output = doc.GetElementById("last_last").innerText
Range("A2").Value = output
ie.Quit
End Sub
请有人能帮我找出如何让这个工作多页?我尝试过搜索,但是没有找到适合我需要的东西
在收集数据的同时,是否有可能弹出一些类似“等待…”的信息
谢谢我发现使用READYSTATE是不可靠的,因为文档有时没有完全加载,或者至少对象模型没有加载 因此,我通常在尝试访问新的doc对象之前添加sleep命令和Doevents 这应该适合你(正如@Dave所说,你不需要使用
IE.Quit
)
我发现使用READYSTATE是不可靠的,因为文档有时没有完全加载,或者至少对象模型没有加载 因此,我通常在尝试访问新的doc对象之前添加sleep命令和Doevents 这应该适合你(正如@Dave所说,你不需要使用
IE.Quit
)
首先,您可能不应该在第二个
导航之前退出ie
实例,其次,您可能需要重复Do..Loop直到以允许浏览器有时间完成加载页面…谢谢,工作得很好,我本来有循环,但它不工作,删除退出ie并对其进行排序。再次感谢。首先,您可能不应该在第二次导航之前退出ie
实例,其次,您可能需要重复Do..Loop直到
以允许浏览器有时间完成页面加载…谢谢,工作得很好,我本来有循环,但它不工作,删除退出ie并对其进行排序。再次感谢。您可以将Range(“A1”).Value=output
和Range(“A2”).Value=output
命令添加到代码顶部的Win32 API休眠函数,以防万一您手头没有。确保将它放在模块的最顶端,所有常量和子例程之上。让我知道这是否适合您。您可以添加范围(“A1”)。Value=output
和Range(“A2”)。Value=output
命令将未添加的Win32 API睡眠函数返回到代码顶部,以防万一您手头没有。确保将它放在模块的最顶端,所有常量和子例程之上。让我知道这是否适合你。
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Extractdatafromwebsite()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Visible = False
ie.Navigate "http://uk.investing.com/commodities/gold"
Do
Sleep 500
DoEvents
Loop Until ie.ReadyState = 4 ' READYSTATE_COMPLETE
Sleep 500
Set doc = ie.Document
output = doc.GetElementById("last_last").innerText
Range("A1").Value = output
ie.Navigate "http://uk.investing.com/commodities/silver"
Do
Sleep 500
DoEvents
Loop Until ie.ReadyState = 4 ' READYSTATE_COMPLETE
Sleep 500
Set doc = ie.Document
output = doc.GetElementById("last_last").innerText
Range("A2").Value = output
ie.Quit
Set ie = Nothing
End Sub