Jump to content

Please help me to get alternate text from links


Recommended Posts

Hi:

I would like to get text from links, if unsuccess then try to get alternate text. The following code does not get alternate text. Can you please tell me how can I do?

Code AutoIt"

#include <Array.au3>

#include <IE.au3>

Global $GetText

$address = InputBox("Get links text", "Please enter a web address:" & @CRLF)

$oIE = _IECreate($address)

$oLinks = _IELinkGetCollection($oIE)

$NumOfLinks = @extended

If $NumOfLinks = 0 Then Exit

Dim $LinkText[1] = [$NumOfLinks]

For $oLink In $oLinks

$GetText = String($oLink.innerText)

If $GetText = "0" Or $GetText <= " " Then

$GetText = _IEPropertyGet($oLink, "alt")

If $GetText = "0" Or $GetText <= " " Then $GetText = "no text"

EndIf

_ArrayAdd($LinkText, $GetText)

Next

_IEQuit($oIE)

_ArrayDisplay($LinkText, "Link text")

Link to comment
Share on other sites

Hi:

I searched From MSDN

http://msdn2.microsoft.com/en-us/library/Aa752084.aspx

Thanks for your help.

Eric Yip

So did I, from the same page as linked to by Dale from the help file. I followed the 'Properties' link from there and still don't see an 'Alt' or 'Alternat Text'. Where do you see it? It appears to be an attribute of an image according to this: HTML Tip: Using ALT And TITLE Attributes:

Understand Their Purpose

The ALT attribute is designed to be an alternative text description for images. ALT text displays before the image is loaded (if it's loaded at all) in the major browsers and instead of the image in text-based browsers like Lynx. ALT is a required element for images and can only be used for image tags because its specific purpose is to describe images.

Post a snippet of the HTML from the page you are working on -- enough to show what the ALT tag looks like and what it's part of. It looks like the issue might be the fact that you are working with the collect of LINK objects. Might be the wrong target...

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi:

I have improve the code so it can get alternate text from graphical links. If something wrong with my code please let me know.

Thank you.

; code AutoIt

#include <Array.au3>

#include <IE.au3>

Global $GetText

$address = InputBox("Get links text", "Please enter a web address:" & @CRLF)

$oIE = _IECreate($address)

$oLinks = _IELinkGetCollection($oIE)

Dim $LinkText[1] = [@extended]

If $LinkText[0] < 1 Then Exit

For $oLink In $oLinks

$GetText = String($oLink.innerText)

If $GetText = "0" Or $GetText <= " " Then

$oTags = _IETagNameGetCollection($oLink, "img")

For $oTag In $oTags

$GetText = String($oTag.alt)

Next

EndIf

If $GetText = "0" Or $GetText <= " " Then $GetText = "no text"

_ArrayAdd($LinkText, $GetText)

Next

_IEQuit($oIE)

_ArrayDisplay($LinkText, "Link text")

; end of code

Eric Yip

Link to comment
Share on other sites

Hi:

I have improve the code so it can get alternate text from graphical links. If something wrong with my code please let me know.

Thank you.

Eric Yip

Well the test is Does It Work?

I don't see why you want the IMG check to be conditional. I also don't think $GetText <= " " is a valid compare operation. So I made a change to get all links' text, and then all images alt:

#include <Array.au3>
#include <IE.au3>

Global $GetText
$address = InputBox("Get links text", "Please enter a web address:" & @CRLF)
$oIE = _IECreate($address)
Dim $LinkText[1]

$oLinks = _IELinkGetCollection($oIE)
If @extended Then
    For $oLink In $oLinks
        $GetText = StringStripWS($oLink.innerText, 3)
        If $GetText = "0" Or $GetText <= " " Then $GetText = "no text"
        _ArrayAdd($LinkText, $GetText)
    Next
EndIf

$oTags = _IETagNameGetCollection($oLink, "img")
If @extended Then
    For $oTag In $oTags
        $GetText = StringStripWS($oTag.alt, 3)
        If $GetText = "0" Or $GetText = "" Then $GetText = "no text"
        _ArrayAdd($LinkText, "Alt:  " & $GetText)
    Next
EndIf

$LinkText[0] = UBound($LinkText) - 1
_IEQuit($oIE)
_ArrayDisplay($LinkText, "Link text")

If that is not what you wanted, just ignore it.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi PsaltyDS

Big thanks for your help. I have learn my code have some mistake.

I copied your code to scite editor and try to run it but unsuccess. The error is "missing EndIf" but I cannot find the problem.

I would like to get all of link's text. If no text found for a link, I would like to get it's alt text.

Sorry for my poor english.

Thank you very much.

Eric Yip

Link to comment
Share on other sites

Hi PsaltyDS

Big thanks for your help. I have learn my code have some mistake.

I copied your code to scite editor and try to run it but unsuccess. The error is "missing EndIf" but I cannot find the problem.

I tested before posting. My code works (though it may not do what you wanted) without errors.

I would like to get all of link's text. If no text found for a link, I would like to get it's alt text.

The problem there is you have no code to match up the link with the img. Once you hit a single link without text, your loop gets alt from ALL the img tags.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Try this:

#include <Array.au3>
#include <IE.au3>

Global $GetText
;$address = InputBox("Get links text", "Please enter a web address:" & @CRLF)
$address = "http://www.autoitscript.com/forum/index.php?showtopic=51133"
$oIE = _IECreate($address, 1)
Dim $LinkText[1]

$oLinks = _IELinkGetCollection($oIE)
If @extended Then
    For $oLink In $oLinks
        $GetText = StringStripWS($oLink.innerText, 3)
        If $GetText = "0" Then
            $oTags = _IETagNameGetCollection($oLink, "IMG")
            If @extended Then
                For $oTag In $oTags
                    $GetText = "ALT: " & $oTag.alt
                Next
            Else
                $GetText = "Not an image, no ALT text."
            EndIf
        EndIf
        _ArrayAdd($LinkText, $GetText)
    Next
EndIf

$LinkText[0] = UBound($LinkText) - 1
_IEQuit($oIE)
_ArrayDisplay($LinkText, "Link text")
Edited by mikehunt114
IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

Try this:

; ...
        If $GetText = "0" Then
            $oTags = _IETagNameGetCollection($oLink, "IMG")
            If @extended Then
                For $oTag In $oTags
                    $GetText = "ALT: " & $oTag.alt
                Next
            Else
                $GetText = "Not an image, no ALT text."
            EndIf
    EndIf
        _ArrayAdd($LinkText, $GetText)oÝ÷ Ûú®¢×¢wÜbv}ý¶IèÃ*.r¥v¥§(*'¡ûZãyËl¢ëh}©bJç-(§q쨺åÊ­¢w­jë¢j'zX§*.v÷öׯzxZ½ëhºÇreyËb¢v§tZ+ü×±¶Z(¥«­¢+ØÀÌØí½1¥¹­Ìô}%1¥¹­Ñ
½±±Ñ¥½¸ ÀÌØí½%¤)%áѹQ¡¸(%½ÈÀÌØí½1¥¹¬%¸ÀÌØí½1¥¹­Ì($$ÀÌØíÑQáÐôMÑÉ¥¹MÑÉ¥Á]L ÀÌØí½1¥¹¬¹¥¹¹ÉQáа̤($%%ÀÌØíÑQáÐôÅÕ½ÐìÀÅÕ½ÐìQ¡¸($$$ÀÌØí½Qô}%Q9µÑ
½±±Ñ¥½¸ ÀÌØí½1¥¹¬°ÅÕ½Ðí%5ÅÕ½Ðì°À¤($$$ÀÌØíÑQáÐôÅÕ½Ðí1PèÅÕ½ÐìµÀìÀÌØí½Q¹±Ð($%¹%($%}ÉÉå ÀÌØí1¥¹­QáаÀÌØíÑQáФ(%9áÐ)¹%

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

That's right, you shouldn't ever get more than one. But hey, who knows... :)

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

That's right, you shouldn't ever get more than one. But hey, who knows... :)

Well, you version only returned the last one, and mine only the first, so neither was going to get more than one anyway...

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

True enough I suppose. Stop pointing out my code flaws :)

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
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...