Extracting a specific element from website source code in VBA

眉间皱痕 提交于 2021-02-10 23:39:44

问题


I'm trying to extract a specific link from a website and I'm having trouble pulling into a String.

The link to the source code is this: view-source:http://finder.fi/yrityshaku/Nokia+oyj this is the part I'm looking at:

<div class="itemName">





      <!-- Yritysnimi -->

        <!-- Aukeaa aina yhteystiedot-vÃ?lilehdelle -->
        <a href="/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia+Oyj/TAMPERE/yhteystiedot/159838" class="resultGray">

I want to extract the Substring:

/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia+Oyj/TAMPERE/yhteystiedot/159838

I've tried to use browserIE.Document.body.innerText but it seems to only copy parts of the source code that are plain text on the original website.

I've researched some but I haven't found a suitable solution yet. Some have suggested pulling just an element from the source code and others copying the whole source code into a string variable. As a person who's not too expert in vba I'd prefer pulling the whole code into a string as I think this way would be easier to understand.

Original website (in finnish) http://finder.fi/yrityshaku/nokia+oyj

A BIG THANK YOU in advance!


回答1:


How about this:

Sub ExtractLink()

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

With IE

    .Navigate "http://finder.fi/yrityshaku/nokia+oyj"

    Do
        DoEvents
    Loop While .Busy Or .Readystate <> 4

    .Visible = True

    Dim oDoc As Object
    Set oDoc = .document

    With oDoc

        Dim cLinks As Object, iLink As Object, sText As String
        sText = "Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia+Oyj/TAMPERE"

        Set cLinks = .links

        For Each iLink In cLinks

            If InStr(1, iLink.Href, sText) Then

                Debug.Print Replace(iLink.Href, "http://finder.fi", "")

            End If

        Next

    End With

End With

End Sub


来源:https://stackoverflow.com/questions/34418092/extracting-a-specific-element-from-website-source-code-in-vba

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