Get URL of image source

moberry

New Member
Joined
Mar 22, 2012
Messages
11
Hi. I am trying to pull the URL of a certain image when using the ie.getelementby... method.

Using the following I can access the url I need but it's surrounded by html code that is proving difficult for me to seperate out:

test = ie.document.getElementById("recaptcha_image").InnerHtml

this is what debug.print test shows me:


<IMG style="DISPLAY: block" alt="reCAPTCHA challenge image" src="http://www.google.com/recaptcha/api/image?c=03AHJ_VuseNxC9vqebkGvR3AamjyyeAaPVUSkJcCkZ6fVvhQbxIOC8YiIZHV9p6-Ay-09daLWUf3BSIeYIHqczZVIKAhdFR_HnqTlEMUXNga81zlDAIJ2fwwM9qMEgLxkXM568OSGCvuiQn-QVI6Ob7y1RWKXpMEjIMA" width=300 height=57>

does anyone have any suggestions or a completely different way I should be looking at the image source?


Just for clarity, I'm pulling an image from a site and pasting it into a specified cell. I already have part 2 working but I have to pass in the necessary url. If I paste in the url from getelementbyid (manually extracting it) the correct image shows up in excel.


I don't believe im violating any terms of service regarding captcha as im still requiring a person to type in the response.


thanks in advance!
 
Last edited:
You should be able to use this to pull out the image URL.
Code:
imgURL = Mid(test, InStr(1, test, Chr(34)) + 1, InStr(InStr(1, test, Chr(34)) + 1, test, Chr(34)) - (InStr(1, test, Chr(34)) + 1))

this pulls everything between the first and second quotation mark [Chr(34)].

this might potentially be the solution, however, it returns:

DISPLAY: block

because that the first part of the string that is in quotations. Is there any way to look at the second set of quotes? and does this require that the url is the same number of characters every time? this is unlikely.

IMG style="DISPLAY: block" alt="reCAPTCHA challenge image" src="http://www.google.com/recaptcha/api/image?c=03AHJ_VuvdLR1Vk1yxn32wgTQLJHPy2hoI3jpT_v6d9HED7VZ4va0GXCwIu_RWSf2kuIjBttiSJ5V_kqLvOGX2lmjcQQ60eT4mZlVIvm0rh23QoLX8zZL6G2EyvPROWo9e1QL70HLqflKLekPcaMe9yaKWYPVhnLNMQQ" width=300 height=57
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
This code will get the URLs of all the images on a page - just change strURL to the page of interest.
Rich (BB code):
Option Explicit
Sub GetAllImageURLs()
Dim ie As Object
Dim doc As Object
Dim lnk As Object
Dim ws As Worksheet
Dim rng As Range
    Set ie = CreateObject("InternetExplorer.Application")
    Set ws = Worksheets.Add
    Set rng = ws.Range("A1")
    With ie
        '.Visible = True
        .Navigate "http://www.mrexcel.com"
        Do Until .ReadyState = 4: DoEvents: Loop
        Set doc = ie.Document

        For Each lnk In doc.getElementsByTagName("IMG")
            rng.Value = lnk.src
            rng.Offset(, 1) = lnk.alt
            Set rng = rng.Offset(1)
        Next lnk

    End With
    ie.Quit
End Sub
 
Upvote 0
I think using .src will be more efficient if you can get it to work as Norie suggests.

If not and you want to use string deconstrutors to break the string down like I showed than this adaptation should give you exactly what you are looking for even if the src is not the first quoted item.

Code:
imgURL = Mid(test, InStr(1, test, "src="))
imgURL = Mid(imgURL, InStr(1, imgURL, Chr(34)) + 1, InStr(InStr(1, imgURL, Chr(34)) + 1, imgURL, Chr(34)) - (InStr(1, imgURL, Chr(34)) + 1))
 
Upvote 0
You can split all the attributes into an array using Split with space as a delimiter.

It shouldn't be too hard to find where the src attribute is in the array.

If you do then all you would need to do would be remove the quotes and 'src=' to get the URL.
 
Upvote 0
I think using .src will be more efficient if you can get it to work as Norie suggests.

If not and you want to use string deconstrutors to break the string down like I showed than this adaptation should give you exactly what you are looking for even if the src is not the first quoted item.

Code:
imgURL = Mid(test, InStr(1, test, "src="))
imgURL = Mid(imgURL, InStr(1, imgURL, Chr(34)) + 1, InStr(InStr(1, imgURL, Chr(34)) + 1, imgURL, Chr(34)) - (InStr(1, imgURL, Chr(34)) + 1))

This returns the exact string I am looking for. I've been fighting with this for days. Thanks


And thanks to everyone that replied in the thread. the response time on this forum is amazing.. ill definitely be spending more time here.
 
Upvote 0
This returns the exact string I am looking for. I've been fighting with this for days. Thanks


And thanks to everyone that replied in the thread. the response time on this forum is amazing.. ill definitely be spending more time here.

No problem and by the way; welcome to the forums.
 
Upvote 0
This code will get the URLs of all the images on a page - just change strURL to the page of interest.
Rich (BB code):
Option Explicit
Sub GetAllImageURLs()
Dim ie As Object
Dim doc As Object
Dim lnk As Object
Dim ws As Worksheet
Dim rng As Range
    Set ie = CreateObject("InternetExplorer.Application")
    Set ws = Worksheets.Add
    Set rng = ws.Range("A1")
    With ie
        '.Visible = True
        .Navigate "MREXCEL – Just another WordPress site"
        Do Until .ReadyState = 4: DoEvents: Loop
        Set doc = ie.Document

        For Each lnk In doc.getElementsByTagName("IMG")
            rng.Value = lnk.src
            rng.Offset(, 1) = lnk.alt
            Set rng = rng.Offset(1)
        Next lnk

    End With
    ie.Quit
End Sub

I know this is a old thread but I need a little help about this message,
that links is not enough, can I get that images in cells ?

Thx
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,678
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top