- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 VBA 抓取一些网站是为了好玩,我使用 VBA 作为工具。我使用 XMLHTTP 和 HTMLDocument(因为它比 internetExplorer.Application 更快)。
Public Sub XMLhtmlDocumentHTMLSourceScraper()
Dim XMLHTTPReq As Object
Dim htmlDoc As HTMLDocument
Dim postURL As String
postURL = "http://foodffs.tumblr.com/archive/2015/11"
Set XMLHTTPReq = New MSXML2.XMLHTTP
With XMLHTTPReq
.Open "GET", postURL, False
.Send
End With
Set htmlDoc = New HTMLDocument
With htmlDoc
.body.innerHTML = XMLHTTPReq.responseText
End With
i = 0
Set varTemp = htmlDoc.getElementsByClassName("post_glass post_micro_glass")
For Each vr In varTemp
''''the next line is important to solve this issue *1
Cells(1, 1) = vr.outerHTML
Set varTemp2 = vr.getElementsByTagName("SPAN class=post_date")
Cells(i + 1, 3) = varTemp2.Item(0).innerText
''''the next line occur 438Error''''
Set varTemp2 = vr.getElementsByClassName("hover_inner")
Cells(i + 1, 4) = varTemp2.innerText
i = i + 1
Next vr
End Sub
我通过*1解决了这个问题cells(1,1) 向我展示接下来的事情
<DIV class="post_glass post_micro_glass" title=""><A class=hover title="" href="http://foodffs.tumblr.com/post/134291668251/sugar-free-low-carb-coffee-ricotta-mousse-really" target=_blank>
<DIV class=hover_inner><SPAN class=post_date>...............
是的,所有的类标签都丢失了“”。只有第一个函数的类有“”我实在不知道为什么会出现这种情况。
//我可以通过 getElementsByTagName("span") 进行分词。但我更喜欢“class”标签......
最佳答案
getElementsByClassName method本身不被视为一种方法;仅父 HTMLDocument 的。如果您想使用它来定位 DIV 元素中的元素,则需要创建一个由该特定 DIV 元素的 .outerHtml 组成的子 HTMLDocument。
Public Sub XMLhtmlDocumentHTMLSourceScraper()
Dim xmlHTTPReq As New MSXML2.XMLHTTP
Dim htmlDOC As New HTMLDocument, divSUBDOC As New HTMLDocument
Dim iDIV As Long, iSPN As Long, iEL As Long
Dim postURL As String, nr As Long, i As Long
postURL = "http://foodffs.tumblr.com/archive/2015/11"
With xmlHTTPReq
.Open "GET", postURL, False
.Send
End With
'Set htmlDOC = New HTMLDocument
With htmlDOC
.body.innerHTML = xmlHTTPReq.responseText
End With
i = 0
With htmlDOC
For iDIV = 0 To .getElementsByClassName("post_glass post_micro_glass").Length - 1
nr = Sheet1.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
With .getElementsByClassName("post_glass post_micro_glass")(iDIV)
'method 1 - run through multiples in a collection
For iSPN = 0 To .getElementsByTagName("span").Length - 1
With .getElementsByTagName("span")(iSPN)
Select Case LCase(.className)
Case "post_date"
Cells(nr, 3) = .innerText
Case "post_notes"
Cells(nr, 4) = .innerText
Case Else
'do nothing
End Select
End With
Next iSPN
'method 2 - create a sub-HTML doc to facilitate getting els by classname
divSUBDOC.body.innerHTML = .outerHTML 'only the HTML from this DIV
With divSUBDOC
If CBool(.getElementsByClassName("hover_inner").Length) Then 'there is at least 1
'use the first
Cells(nr, 5) = .getElementsByClassName("hover_inner")(0).innerText
End If
End With
End With
Next iDIV
End With
End Sub
虽然其他 .getElementsByXXXX 可以轻松检索另一个元素中的集合,但 getElementsByClassName method需要考虑它所认为的 HTMLDocument 作为一个整体,即使你欺骗了它这样认为。
关于vba、getElementsByClassName、HTMLSource的双引号不见了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302502/
当我想检查 Explorer 是否存在 getElementsByClassName 时。我在代码下使用并且运行良好。 function getClass(obj) { if (document.
我正在尝试编写一个 PowerShell 脚本,以从网站上获取所有名为“newstitle”的类中的文本。 这就是我所拥有的: function check-krpano { $geturl=
我正在尝试在按钮单击时切换“覆盖”。在同一个类下添加这两个按钮并调用此函数后,JS 根本无法完成它。我不确定问题出在哪里: document.getElementsByClassName( 'trig
这个问题已经有答案了: Why does jQuery or a DOM method such as getElementById not find the element? (7 个回答) 已关闭
将尽力使其尽可能简洁。 使用 VB.Net 中的浏览器控件运行 Javascript 来循环浏览网页上的元素。 我已经让它大部分工作了,这将解决我过去几周的问题,我真的很感激这里的指点。 到目前为止:
我遇到了一个小问题,我想查询我的数据库,这样当用户使用自动完成功能时,我可以在名为 *Exist Act * 的另一列“td”上获取该产品的库存,现在好了使用这个新脚本,当我移出任何文本框时,它会更改
这个问题已经有答案了: Setting innerHTML vs. setting value with Javascript (8 个回答) 已关闭 6 年前。 我试图将日期附加到具有相同类的四个输
大家好,我想做这样的事情: function Div(data){ var A=document.getElementsByClassName('title')[data]; A.
我有一个网页爬虫,我正在使用 puppeteer 来爬行。我尝试使用 getElementsByClassName 获取 DOM 中的所有元素,但它返回了一个不稳定的集合。我期望收到一组 html 元
我有一个带有 TreeMap 片的网站,然后我使用 Ajax 删除该树并使用 javascript 插入数字。我用的是; document.getElementById("cut_oak_tree")
这个问题已经有答案了: Strange behavior when iterating over HTMLCollection from getElementsByClassName (3 个回答)
似乎每次都从闭包外部重新执行getElementsByClassName。 我可以通过将元素复制到数组中来解决这个问题,但我不知道是否有更好的方法。 这是一个代码示例: HTML
我用 VBA 抓取一些网站是为了好玩,我使用 VBA 作为工具。我使用 XMLHTTP 和 HTMLDocument(因为它比 internetExplorer.Application 更快)。 Pu
我一直在尝试使用以下指南进行简单的表单设置:https://www.w3schools.com/howto/howto_js_form_steps.asp. 我在表单内有三个带有 class="tab
我的代码不起作用,我不知道为什么。 var getElementsByClassName = function(className){ let result = []; let child =
我有一些元素属于“预览”类。我想让其中一个可以点击,所以首先我想通过 找出它在 HTML 集合中的索引号 console.log(document.getElementsByClassName("pr
我有一个问题,为什么我不想工作一段时间很累。我刚开始学js。非常感谢您的帮助
我正在使用 SnackJS API。我需要将一个事件附加到每个输入元素(文本框),类名称为“qty”。我无法为此使用 id 属性,因为它是动态生成的并且是唯一的,并且正在被其他东西使用: s
这个问题在这里已经有了答案: What do querySelectorAll and getElementsBy* methods return? (12 个答案) 关闭 8 年前。 我只想打印属
我想得到返回类名的长度 我想为一系列按钮添加事件监听器,它们都共享相同的类名。 removeButton = document.getElementsByClassName("removeButton
我是一名优秀的程序员,十分优秀!