- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Excel VBA 取消汇率,但无法获得我需要的innerText 值。我不明白为什么,因为同样的技术也适用于其他网站。
网址 - https://www.nbs.rs/export/sites/default/internet/english/scripts/kl_srednji.html
Sub GetCurr()
Dim tempHTMLDoc As New MSHTML.HTMLDocument
Dim HTMLCurrency As MSHTML.IHTMLElementCollection
Dim HTMLRows As MSHTML.IHTMLElementCollection
Dim HTMLDate As MSHTML.IHTMLElementCollection
Dim HTMLElem As MSHTML.IHTMLElement
Dim connectionTest As Boolean
Dim EUR, CZK, HRK, HUF, PLN, RON, RSD As String
Dim myURL As String
Dim i As Long
connectionTest = True
myURL = "https://www.nbs.rs/export/sites/default/internet/english/scripts/kl_srednji.html"
Call WebConnection(tempHTMLDoc, connectionTest, myURL)
If connectionTest = False Then Exit Sub
Set HTMLDate = tempHTMLDoc.getElementsByTagName("span")
'Debug.Print HTMLDate.Length
For Each HTMLElem In HTMLDate 'I am looking for which element contains the date (can not find)
Debug.Print HTMLElem.innerText
Next HTMLElem
'I am trying to get the necessary currencies
Set HTMLRows = tempHTMLDoc.getElementsByTagName("tr")
Debug.Print HTMLRows.Length
For i = 0 To HTMLRows.Length - 1 'If lenght > 0
Set HTMLCurrency = HTMLRows(i).getElementsByTagName("td")
If HTMLCurrency.Length > 4 Then 'each currency contains 5 "td" tags
Select Case HTMLCurrency(2).innerText
Case "EUR"
EUR = HTMLCurrency(4).innerText
Case "HRK"
HRK = HTMLCurrency(4).innerText
Case "HUF"
HUF = HTMLCurrency(4).innerText
Case "PLN"
PLN = HTMLCurrency(4).innerText
Case "RON"
RON = HTMLCurrency(4).innerText
Case "CZK"
CZK = HTMLCurrency(4).innerText
End Select
End If
Next i
Debug.Print "EUR - ", EUR; vbNewLine; "HRK - ", HRK; vbNewLine; "HUF - ", HUF; vbNewLine; "PLN - ", PLN; vbNewLine; _
"RON - ", RON; vbNewLine; "CZK - ", CZK
End Sub
'============================================================================
Sub WebConnection(HTMLDoc As MSHTML.HTMLDocument, ConnTest As Boolean, URL As String)
Dim XMLPage As New MSXML2.XMLHTTP60
Dim errorMsg As VbMsgBoxResult
On Error GoTo CONNECTION_ERROR
XMLPage.Open "GET", URL, False
XMLPage.send
On Error GoTo 0
If XMLPage.Status <> 200 Then
errorMsg = MsgBox("There is something wrong with webpage. Do you want to try to continue?", vbYesNo + vbCritical, "ERROR")
If errorMsg = vbNo Then
ConnTest = False
Exit Sub
End If
End If
HTMLDoc.body.innerHTML = XMLPage.responseText
Exit Sub
CONNECTION_ERROR:
MsgBox "There is something wrong with the connection.", vbCritical, "ERROR"
ConnTest = False
Exit Sub
End Sub
我尝试使用 id (index:srednjiKursList:tbody_element) 或类名(tableCell) 但它不起作用。该网站以不同的方式构建
最佳答案
您的原始链接(我们称之为登录页面)是动态加载的。您的 GET
请求太快,无法检索所需的信息。
您可以使用另一个 URL。
当您转到登陆页面时,您会发现它实际上发出了 XMLHTTP GET
请求到以下页面:
以上内容来自使用 fiddler
但您可以使用 Chrome 开发工具 (F12) 等工具检查网络流量。
您可以将该 URL 直接输入到代码中,并且效果完美。
<小时/>整个表格:
您还可以按如下方式获取整个表格:
Option Explicit
Public Sub GetInfo()
Dim html As New HTMLDocument, hTable As HTMLTable, clipboard As Object
Const URL = "https://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=eng"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
html.body.innerHTML = StrConv(.responseBody, vbUnicode)
End With
Set hTable = html.getElementById("index:srednjiKursLista")
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
clipboard.SetText hTable.outerHTML
clipboard.PutInClipboard
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).PasteSpecial
End Sub
<小时/>
结果示例:
<小时/><小时/>仅列出的货币:
您还可以根据表结构使用一些数学知识来获取您列出的那些元素。
Option Explicit
Public Sub GetInfo()
Dim html As New HTMLDocument, hTable As HTMLTable, clipboard As Object
Const URL = "https://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=eng"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
html.body.innerHTML = StrConv(.responseBody, vbUnicode)
End With
Set hTable = html.getElementById("index:srednjiKursLista")
Dim list As Object, i As Long
Dim EUR As Double, CZK As Double, HRK As Double, HUF As Double, PLN As Double, RON As Double, RSD As Double
Set list = hTable.querySelectorAll("td")
For i = 2 To list.Length - 1 Step 5
Select Case list.item(i).innerText
Case "EUR"
EUR = list.item(i + 2).innerText
Case "HRK"
HRK = list.item(i + 2).innerText
Case "HUF"
HUF = list.item(i + 2).innerText
Case "PLN"
PLN = list.item(i + 2).innerText
Case "RON"
RON = list.item(i + 2).innerText
Case "CZK"
CZK = list.item(i + 2).innerText
End Select
Next
Debug.Print "EUR - ", EUR; vbNewLine; "HRK - ", HRK; vbNewLine; "HUF - ", HUF; vbNewLine; "PLN - ", PLN; vbNewLine; _
"RON - ", RON; vbNewLine; "CZK - ", CZK
End Sub
<小时/>
使用剪贴板:
以下行:
GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
添加对 Microsoft Forms 对象库的后期绑定(bind)引用,以便您可以访问剪贴板。
您还可以将用户窗体添加到您的项目中,或者转至 VBE > 工具 > 引用 > Microsoft Forms 对象库来访问:
关于excel - VBA - 网页抓取无法获取 HTMLElement insideText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52028019/
我不熟悉使用javascript。我使用了innertext。但是浏览器不会显示该内容,请帮助我.. function Show(name) { document.getElementById("n
我有一个 JS 函数在 A 标签上的 onclick 事件后激活。这是代码: (function(MTM) { MTM.selectAllNone = function(e) { var
如何获取 QWebView 上的所有 document.body.innerText?我使用QT版本5.5.1。 例如, 要加载的html文件 document.write("hello w
我正在尝试使用 Excel VBA 取消汇率,但无法获得我需要的innerText 值。我不明白为什么,因为同样的技术也适用于其他网站。 网址 - https://www.nbs.rs/export/
我有一个带有 contenteditable 的简单 div,我想在没有 HTML 的情况下存储它的值,但保留换行符,所以我使用 innerText。 问题:是否可以直接将其保存在数据库中,还是应该在
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: InnerText alternative in mozilla 我有这个代码 My programIt is he
这个问题已经有答案了: 'innerText' works in IE, but not in Firefox (15 个回答) 已关闭 7 年前。 这是我的 JavaScript 代码: funct
我需要从此网址读取“上传日期”: https://www.youtube.com/tv?vq=high#/watch/video/control?v=hqwU7nv3hTM&resume 使用此代码:
我使用 div 供人们输入文本,然后我尝试保存 div.innerText 和 div.innerHTML 到我的数据库,但是当我将其从数据库带回并将其放回 div 时,所有回车符或换行符都消失了 i
这是我的示例源 HTML: First Middle
我正在学习 Javascript,但遇到了一个问题: 我可以设置段落元素的innerText。但立即,网络浏览器撤消就是我的工作了!这意味着网页完全恢复到原来的状态,就好像我重新加载了该页面一样。
我有以下结构: Tag 1 Tag 2 Tag 3 我正在尝试将用户输入与#campaignTags的每个子项的innerText进行匹配 这是我将节点与用户输入 jQue
我遇到这样的问题: happi After write : var thevalue = document.getElementById("stickyid").innerText; documen
我正在尝试使用 Javascript 捕获页面以下 HTML 中的值“Lusaka” Outbound flight
我在 JS 中遇到了一个小问题。我需要使用 for 函数创建大约七个 DIV,但每个 div 的内容(这里是 insideText)必须不同。有没有办法用函数来做到这一点,而不需要手动修改每个 div
我知道对网页 DOM 的某些更改(通过 JavaScript)将导致渲染引擎计算新的渲染布局(不确定我的术语是否正确)。 我有一个进程,可以通过 cometd 式的长轮询请求相当频繁地更新数据。其中一
我有一个用 Materialise CSS 制作的下拉菜单。我试图在用户单击“按钮”时更改其上的文本,但由于某种原因它不会更新 DOM。 var changeText; changeText = fu
我们正在使用 javascript/typescript,并且无法从 DevExtreme (Typescript) 项目中的 dxTextArea 传递“输入文本”。 我们正在使用: 函数“
我正在从事评论网站的客户端工作。 用户可以在评论描述中插入任何文本,包括 html 元素文本(例如: console.log('hello') ),并使用基本文本样式:斜体、粗体等... 我想测试
我正在从事评论网站的客户端工作。 用户可以在评论描述中插入任何文本,包括 html 元素文本(例如: console.log('hello') ),并使用基本文本样式:斜体、粗体等... 我想测试
我是一名优秀的程序员,十分优秀!