Jump to content

Trying to get the href of an image


EvAsion
 Share

Recommended Posts

How can i get the link that an image points to in an internet explorer object?

I was thinking:

$oInputs = _IETagNameGetCollection ($oIE, "img")
$sHtml = _IEBodyReadHTML($oIE)
;ClipPut($sHtml)
$blah = StringRegExp($sHtml,'target=_xtr><IMG alt=["]{0,1}(.*?)["]{0,1} src',1)
For $a In $oInputs
    ;msgbox(0,"",$blah[0]&"   "&$a.alt&"     "&$a.href)
    If Not $a.alt = 0 Then
        If $a.alt = $blah[0] Then 
            msgbox(0,"",$a.href)
            ;_IECreate($a.href)
            ExitLoop
        EndIf
    EndIf
Next

But $a.href just returns the src of the image(same as $a.src)

Link to comment
Share on other sites

Mmm.. this works for me:

$oHTTP = ObjCreate("Microsoft.XMLHTTP")
$oHTTP.Open("GET", "http://www.autoitscript.com/", False)
$oHTTP.Send()
$oString = StringRegExp($oHTTP.ResponseText, '<a href="(.*?)"><img', 3)
For $i = 0 To UBound($oString) -1
    MsgBox(0,"",$oString[$i])
Next

I'm not sure about the matching pattern in the StringRegExp function though...

Link to comment
Share on other sites

  • Moderators

The href property you are looking for is actually that of the parent anchor (a). Here is an example of how to access it.

#include <IE.au3>

$iIndex = 0
$sInfo = ""
$sURL = "www.autoitscript.com"

_IEErrorHandlerRegister()
$oIE = _IECreate($sURL)
$oIMGs = _IEImgGetCollection($oIE)
ConsoleWrite("Total of " & @extended & " images" & @CR & @CR)
For $oIMG In $oIMGs
    $sInfo &= "Index: " & $iIndex & @CR
    $sInfo &= @TAB & "Alt: " & $oIMG.alt & @CR
    $sInfo &= @TAB & "Name: " & $oIMG.nameProp & @CR
    $sInfo &= @TAB & "SRC: " & $oIMG.src & @CR
    $oLink = $oIMG.parentElement
    If $oLink.tagName = "a" Then
        $sInfo &= @TAB & "Link: " & $oLink.href & @CR
    EndIf
    $sInfo &= @CR
    $iIndex += 1
Next
ConsoleWrite($sInfo)
Link to comment
Share on other sites

Oh cool :) I really want to learn how to work with objects properly. This is what I had done, which I thought was closer to what he wanted:

#include <IE.au3>
$link="http://www.autoitscript.com/"
$oIE=_IECreate($link)
$sHtml = _IEBodyReadHTML($oIE)
$blah = StringRegExp($sHtml,'(?i)<a href="(.*?)"><img',3)
For $a=0 To UBound($blah)-1
    MsgBox(0,"Image Link Found",$blah[$a])
    $check=StringSplit($blah[$a],":")
    If $check[1]="http" Then
        MsgBox(0,"","Going to: "&$blah[$a])
        _IECreate($blah[$a])
    Else
        MsgBox(0,"","Going to: "&$link&$blah[$a])
        _IECreate($link&$blah[$a])
    EndIf
Next
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...