【发布时间】:2020-12-12 11:49:13
【问题描述】:
如何使用VBA excel 2007 从资源中获取og:image
例如,这个网址:
https://www.bbc.com/reel/video/p08jgfdg/the-truth-about-christopher-columbus
【问题讨论】:
-
请包括您的编码尝试
标签: excel vba excel-2007
如何使用VBA excel 2007 从资源中获取og:image
例如,这个网址:
https://www.bbc.com/reel/video/p08jgfdg/the-truth-about-christopher-columbus
【问题讨论】:
标签: excel vba excel-2007
使用更新版本的 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
如果您想要一个仅将 URL 作为字符串返回的函数,您可以轻松编辑 Sub 过程,使其成为一个以 MyUrl 作为参数并返回字符串而不是使用它来插入图像的函数Activesheet (like this for example)。
【讨论】:
Excel-2007 标签。我添加了一些内容,使其适用于该版本的 Excel。
HttpRequest.Send 上中断...无论如何...有没有可能成为一个函数,以便我可以从任何单元格获取MyUrl,例如:=GetImageFromHead(A3)?
MyUrl作为参数来做到这一点,但是您希望函数返回什么?字符串形式的图像 URL?