为什么不使用 Internet Explorer 附带的 MSHTML 解析网页?您需要添加对“Microsoft HTML 对象库”的引用。奇怪的是,您必须实例化一个空的 HTMLDocument 对象,然后通过调用第一个对象的方法基于您的 URL 创建一个新的 HTMLDocument 对象。但是您必须保留原始对象,否则您会不断收到“Permission Denied”错误消息。我仍然将它们都放入一个 UDT 以确保它们保持在彼此相同的范围内。
Option Explicit
Private Type HtmlDoc
Parent As MSHTML.HTMLDocument
Main As MSHTML.HTMLDocument
End Type
Private Sub Command1_Click()
Dim URL
Dim uHTMLDoc As HtmlDoc
URL = "http://www.youtube.com/results?search_query=" & TextBox1.Text & "&suggested_categories=2%2C23%2C25&page=" & pagenum
' Source Code
GetHTMLDocumentFromURL URL, uHTMLDoc
Debug.Print uHTMLDoc.Main.documentElement.outerHTML
End Sub
Private Sub GetHTMLDocumentFromURL(ByRef the_sURL As String, ByRef the_uHTMLDoc As HtmlDoc)
With the_uHTMLDoc
Set .Parent = New MSHTML.HTMLDocument
Set .Main = .Parent.createDocumentFromUrl(the_sURL, vbNullString)
' Wait for the document to load completely.
' This is because the transfer is asynchronous.
' It is possible that this string might be different if you have another
' language than English for Internet Explorer on the
' machine where the code is executed.
Do While .Main.readyState <> "complete"
DoEvents
Loop
End With
End Sub
我不知道你想做什么类型的解析,但是看看 HTMLDocument 类上的各种方法,例如 GetElementById()、GetElementsByName() 和 GetElementsByTagName()。好好看看类型库,并尝试一些实验来掌握它。