gpt4 book ai didi

javascript - VBA动态网页抓取Excel

转载 作者:行者123 更新时间:2023-12-03 02:50:57 27 4
gpt4 key购买 nike

我有一个关于如何从此网页抓取数据的问题:

http://tvc4.forexpros.com/init.php?family_prefix=tvc4&carrier=64694b96ed4909e815f1d10605ae4e83&time=1513525898&domain_ID=70&lang_ID=70&timezone_ID=31&pair_ID=171&interval=86400&refresh=4&session=session&client=1&user=200743128&width=650&height=750&init_page=instrument&m_pids=&watchlist=&site=https://au.investing.com&version=1.11.2

它似乎被保存在 iframe 中,并且屏幕上出现了一堆 JavaScript。

每当我尝试收集 iframe 下的 span 或 div 或 tr 标签中的元素时,我似乎无法收集其中的数据。

我的目标是 class="pane-legend-item-value pane-legend-line main"元素内保存的内部文本。

显然,内部文本会根据特定时间光标在屏幕上的位置而变化。所以我尝试做的是设置一个 IE,它已经加载了页面并且光标位于正确的位置,在图表的末尾(给我最后一个数据点),然后你可以将光标移出屏幕,然后我编写了一些简单的代码来抓取该 IE 窗口,然后尝试了 GetElements,此时我无法获取任何数据。

这是我到目前为止的代码,它非常粗糙,因为我在阅读更多选项时一直在尝试编辑,但没有任何胜利:( ...任何想法或帮助将不胜感激!(屏幕截图是也在底部)

Sub InvestingCom()

Dim IE As InternetExplorer
Dim htmldoc As MSHTML.IHTMLDocument 'Document object
Dim eleColth As MSHTML.IHTMLElementCollection 'Element collection for th tags
Dim eleColtr As MSHTML.IHTMLElementCollection 'Element collection for tr tags
Dim eleColtd As MSHTML.IHTMLElementCollection 'Element collection for td tags
Dim eleRow As MSHTML.IHTMLElement 'Row elements
Dim eleCol As MSHTML.IHTMLElement 'Column elements
Dim elehr As MSHTML.IHTMLElement 'Header Element
Dim iframeDoc As MSHTML.HTMLDocument
Dim frame As HTMLIFrame
Dim ieURL As String 'URL

'Take Control of Open IE
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title

If my_title Like "*" & "*" Then 'compare to find if the desired web page is already open
Set IE = objShell.Windows(x)
marker = 1
Exit For
Else
End If
Next

'Extract data
Set htmldoc = IE.document 'Document webpage

' I have tried span, tr, td etc tags and various other options
' I have never actually tried collecting an HTMLFrame but googled it however was unsuccessful
End Sub

Excel 可以找到并与在另一个屏幕上打开的 Excel 和 VB 进行对话的现有 IE 的屏幕截图以及我想要抓取的数据

Screenshot of the already existing IE which excel can find and talk to with excel and VB open on the other screen and the data I would like to scrape

最佳答案

对我来说,处理该页面中的两个嵌套的 iframe 来收集所需的内容确实很困难。但无论如何,我终于把它修好了。运行以下代码并获取您请求的内容:

Sub forexpros()
Dim IE As New InternetExplorer, html As HTMLDocument
Dim frm As Object, frmano As Object, post As Object

With IE
.Visible = True
.navigate "http://tvc4.forexpros.com/init.php?family_prefix=tvc4&carrier=64694b96ed4909e815f1d10605ae4e83&time=1513525898&domain_ID=70&lang_ID=70&timezone_ID=31&pair_ID=171&interval=86400&refresh=4&session=session&client=1&user=200743128&width=650&height=750&init_page=instrument&m_pids=&watchlist=&site=https://au.investing.com&version=1.11.2"
Do Until .readyState = READYSTATE_COMPLETE: Loop
Application.Wait (Now + TimeValue("0:00:05"))
Set frm = .document.getElementsByClassName("abs") ''this is the first iframe
.navigate frm(0).src
Do Until .readyState = READYSTATE_COMPLETE: Loop
Application.Wait (Now + TimeValue("0:00:05"))
Set html = .document
End With

Set frmano = html.getElementsByTagName("iframe")(0).contentWindow.document ''this is the second iframe

For Each post In frmano.getElementsByClassName("pane-legend-item-value pane-legend-line main")
Debug.Print post.innerText
Next post
IE.Quit
End Sub

关于javascript - VBA动态网页抓取Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47861303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com