• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Expecting a little twitch to manipulate string Conditionally

shahin

Active Member
Hi there all! Hope you are doing well. I would like to parse links from web which meet a certain condition. I would like to parse links only which contain "about:/wiki" in its first position, as in "about:/wiki/wikipedia". I suppose, if statement can do the magic. If anyone helps me do it, I would be very grateful. Thanks in advance. By the way, I've uploaded an image to make you understand what i meant, specially when you see the difference between the first two links and the rest.

Code:
Sub ConditionalLink()
Const url = "https://en.wikipedia.org/wiki/Main_Page"
Dim Links As Object, Link As Object

With CreateObject("MSXML2.serverXMLHTTP")
    .Open "GET", url, False
    .setRequestHeader "Content-Type", "text/xml"
    .send
    Set html = CreateObject("htmlfile")
    html.body.innerHTML = .responseText
End With
Set Links = html.getElementsByTagName("a")
    For Each Link In Links
        x = x + 1
        Cells(x, 1) = Link.href
    Next Link
Set Links = Nothing
End Sub
 

Attachments

  • Untitledq.jpg
    Untitledq.jpg
    22.5 KB · Views: 3
Last edited:
You can use Instr function.

Like...
Code:
Sub ConditionalLink()
Const url = "https://en.wikipedia.org/wiki/Main_Page"
Dim Links As Object, Link As Object

With CreateObject("MSXML2.serverXMLHTTP")
    .Open "GET", url, False
    .setRequestHeader "Content-Type", "text/xml"
    .send
    Set HTML = CreateObject("htmlfile")
    HTML.body.innerHTML = .responseText
End With
Set Links = HTML.getElementsByTagName("a")
    For Each Link In Links
        If InStr(1, Link.href, "about:/wiki/") > 0 Then
            x = x + 1
            Cells(x, 1) = Link.href
        End If
    Next Link
Set Links = Nothing
End Sub
 
Hi sir Chihiro, nice to have you in my thread. The solution you provided is exactly what I was expecting to have. Thanks a trillion.
 
Btw, what is the 1 for in InStr function? Forgive my ignorance. Is it always has to be 1 for a single string, I meant link?
 
Back
Top