我用vba刮了一些网站以获得乐趣,并使用VBA作为工具。我使用XMLHTTP和HTMLDocument(因为它比internetExplorer.Application更快)。vba,getElementsByClassName,HTMLSource的双引号不见了
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个 细胞(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”)进行分析。但我更喜欢“类”标记.....
http://stackoverflow.com/questions/7927905/internet-explorer-innerhtml-outputs-attributes-without-quotes我不认为HTML需要引号属性值时,值不包含空格,并且你是什么看看何时看到outerHTML反映了IE对此的表示。这可能不是你所得到的错误的根源。 –
如果您尝试设置varTemp2 = vr.querySelectorAll(“span.post_date”)'会发生什么? – barrowc
感谢所有! @TimWilliams我明白了。那么getElementsByTagName(“span”)是我可以分析innerText的唯一方法? – Soborubang