如何使用 VBA excel 从资源中获取 og:image
How can I get og:image from resource with VBA excel
如何使用 VBA
excel 2007
从资源中获取 og:image
例如,这个 URL:
https://www.bbc.com/reel/video/p08jgfdg/the-truth-about-christopher-columbus
使用较新版本的 Excel,您可以试试这个:
Sub GetImageFromHead()
Dim MyUrl As String
MyUrl = "https://www.bbc.com/reel/video/p08jgfdg/the-truth-about-christopher-columbus"
'Required library reference: Microsoft XML v6.0
Dim HttpRequest As MSXML2.XMLHTTP60
Set HttpRequest = New MSXML2.XMLHTTP60
HttpRequest.Open "GET", MyUrl, False
HttpRequest.Send
Dim HtmlDoc As Object
Set HtmlDoc = CreateObject("htmlfile")
HtmlDoc.Write HttpRequest.responseText
'This next line makes sure that the JavaScript on the page gets processed before continuing execution of the code.
DoEvents
'Required library reference: Microsoft HTML Object
Dim MetaCollection As MSHTML.IHTMLElementCollection
Set MetaCollection = HtmlDoc.getElementsByTagName("meta")
Dim HtmlElement As MSHTML.IHTMLElement
For Each HtmlElement In MetaCollection
If HtmlElement.getAttribute("property") = "og:image" Then
ActiveSheet.Pictures.Insert (HtmlElement.getAttribute("content"))
End If
Next
End Sub
但是由于您的问题是针对 Excel 2007 年,因此您必须像这样定义 HttpRequest
:
'Required library reference: Microsoft XML v3.0 or v5.0
Dim HttpRequest As MSXML2.XMLHTTP
Set HttpRequest = New MSXML2.XMLHTTP
如果您想要一个函数,它只是将 return 和 URL 作为一个字符串来代替,您可以轻松地编辑 Sub 过程,使其成为一个以 MyUrl 作为参数并且 return使用字符串而不是使用它在 Activesheet 中插入图像 (like this for example)。
如何使用 VBA
excel 2007
og:image
例如,这个 URL:
https://www.bbc.com/reel/video/p08jgfdg/the-truth-about-christopher-columbus
使用较新版本的 Excel,您可以试试这个:
Sub GetImageFromHead()
Dim MyUrl As String
MyUrl = "https://www.bbc.com/reel/video/p08jgfdg/the-truth-about-christopher-columbus"
'Required library reference: Microsoft XML v6.0
Dim HttpRequest As MSXML2.XMLHTTP60
Set HttpRequest = New MSXML2.XMLHTTP60
HttpRequest.Open "GET", MyUrl, False
HttpRequest.Send
Dim HtmlDoc As Object
Set HtmlDoc = CreateObject("htmlfile")
HtmlDoc.Write HttpRequest.responseText
'This next line makes sure that the JavaScript on the page gets processed before continuing execution of the code.
DoEvents
'Required library reference: Microsoft HTML Object
Dim MetaCollection As MSHTML.IHTMLElementCollection
Set MetaCollection = HtmlDoc.getElementsByTagName("meta")
Dim HtmlElement As MSHTML.IHTMLElement
For Each HtmlElement In MetaCollection
If HtmlElement.getAttribute("property") = "og:image" Then
ActiveSheet.Pictures.Insert (HtmlElement.getAttribute("content"))
End If
Next
End Sub
但是由于您的问题是针对 Excel 2007 年,因此您必须像这样定义 HttpRequest
:
'Required library reference: Microsoft XML v3.0 or v5.0
Dim HttpRequest As MSXML2.XMLHTTP
Set HttpRequest = New MSXML2.XMLHTTP
如果您想要一个函数,它只是将 return 和 URL 作为一个字符串来代替,您可以轻松地编辑 Sub 过程,使其成为一个以 MyUrl 作为参数并且 return使用字符串而不是使用它在 Activesheet 中插入图像 (like this for example)。