我用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