how to get the meta name keywords -vba

偶尔善良 提交于 2019-12-13 04:53:25

问题


I am trying to get the meta name keywords from a webpage

meta name="keywords" content="Mitch Albom,For One More Day,Little, Brown Book Group,0751537535,Fiction / General,General & Literary Fiction,Modern & contemporary fiction (post c 1945),USA

I need to get the contents from it need help.

Option Explicit

Sub GetData()
    Dim ie As New InternetExplorer
    Dim str As String
    Dim wk As Worksheet
    Dim webpage As New HTMLDocument
    Dim item As HTMLHtmlElement

    Set wk = Sheet1
    str = wk.Range("Link").value
    ie.Visible = True

    ie.Navigate str

    Do
        DoEvents
    Loop Until ie.ReadyState = READYSTATE_COMPLETE

    Dim Doc As HTMLDocument
    Set Doc = ie.Document

    Dim kwd As String
    kwd = Trim(Doc.getElementsByTagName("keywords").innerText)
    MsgBox kwd

End Sub

回答1:


The best way to do that is by finding meta-element with name keyword and referring to its content property. You can do it like that:

Option Explicit

Sub GetData()
    Dim ie As New InternetExplorer
    Dim str As String
    Dim wk As Worksheet
    Dim webpage As New HTMLDocument
    Dim item As HTMLHtmlElement

    Set wk = Sheet1
    str = wk.Range("Link").value
    ie.Visible = True

    ie.Navigate str

    Do
        DoEvents
    Loop Until ie.ReadyState = READYSTATE_COMPLETE


    'Find the proper meta element --------------
    Const META_TAG As String = "META"
    Const META_NAME As String = "keywords"
    Dim Doc As HTMLDocument
    Dim metaElements As Object
    Dim element As Object
    Dim kwd As String


    Set Doc = ie.Document
    Set metaElements = Doc.all.tags(META_TAG)

    For Each element In metaElements
        If element.Name = META_NAME Then
            kwd = element.Content
        End If
    Next

    MsgBox kwd

End Sub


来源:https://stackoverflow.com/questions/34357705/how-to-get-the-meta-name-keywords-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!