Getting href data, why is it so hard? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Georgie Munteer

Getting href data, why is it so hard?

20 Nov 2017, 09:43

here is part of the code

all i want is that it should also give me the link that the text goes to, i searched and found all kinds of ideas about outerhtml and so out but it does not work

what is the secret?

loop, 18
data42 .= pwb.document.getElementsByClassName("harsg-uli")[ A_index -1 ].innerText "`n"

what do I use to get the link that the title goes too (the above for example is a title called "ABC 987" that links to a url

Please dont tell me that the solution is to download the file and run regex :-(
Georgie Munteer

Re: Getting href data, why is it so hard?

20 Nov 2017, 10:31

Blackholyman wrote:Is that all of your code?
sorry for not pointing it out. I have all the code to load the page and it is working fine, it is getting me all the data i need.

Just need to know, is there anything i can do so when it gets me the titles it gets me the url it points to?

Technically if i go to a webpage and mouse over a link i see the url, why cant COM get me this data [title + link]
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Getting href data, why is it so hard?

20 Nov 2017, 10:38

I initially thought that to get a url from an href, you had to parse the innerText, but this is not true, I saw someone use .href on the forum somewhere. Note: if you do .href on an element that doesn't have an href, you get an error message, hence the use of 'try' to avoid the error message.

Code: Select all

;example website to retrieve urls from:
;AutoHotkey Community - Index page
;https://autohotkey.com/boards/

;[WBGet function]
;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

q:: ;get urls from 'a' elements
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
vOutput := ""
vEltTag := "a"
oElts := oWB.document.getElementsByTagName(vEltTag)
Loop, % oElts.length
{
	oElt := oElts[A_Index-1]
	vHref := ""
	try vHref := oElt.href
	if !(vHref = "")
		vOutput .= vHref "`r`n"
}
oWB := oElts := oElt := ""
MsgBox, % Clipboard := vOutput
return
Last edited by jeeswg on 09 Dec 2018, 20:24, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Georgie Munteer

Re: Getting href data, why is it so hard?

20 Nov 2017, 11:00

jeeswg wrote:I initially thought that to get a url from an href, you had to parse the innerText, but this is not true, I saw someone use .href on the forum somewhere. Note: if you do .href on an element that doesn't have an href, you get an error message, hence the use of 'try' to avoid the error message.

Code: Select all

;example website to retrieve urls from:
;AutoHotkey Community - Index page
;https://autohotkey.com/boards/

;[WBGet function]
;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

q:: ;get urls from 'a' elements
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
vOutput := ""
vEltTag := "a"
Loop, % oWB.document.getElementsByTagName(vEltTag).length
{
	oElt := oWB.document.getElementsByTagName(vEltTag)[A_Index-1]
	vHref := ""
	try vHref := oElt.href
	if !(vHref = "")
		vOutput .= vHref "`r`n"
}
MsgBox, % Clipboard := vOutput
return
The .href has not worked in years :-( doubt you will find any modern website that .href will give you anything but errors :headwall:
User avatar
Blackholyman
Posts: 1282
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark
Contact:

Re: Getting href data, why is it so hard?

20 Nov 2017, 11:14

Georgie Munteer wrote:
The .href has not worked in years :-( doubt you will find any modern website that .href will give you anything but errors :headwall:
Works just fine
Georgie Munteer

Re: Getting href data, why is it so hard?  Topic is solved

20 Nov 2017, 11:21

Blackholyman wrote:
Georgie Munteer wrote:
The .href has not worked in years :-( doubt you will find any modern website that .href will give you anything but errors :headwall:
Works just fine
works on some i should have said
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Getting href data, why is it so hard?

20 Nov 2017, 12:39

jeeswg wrote:Note: if you do .href on an element that doesn't have an href, you get an error message, hence the use of 'try' to avoid the error message.
Alternatively one can use the getAttribute method. Besides, it has the advantage to work with data-* attributes:

Code: Select all

html := "<!DOCTYPE html>"
		. "<html>"
			. "<head>"
			. "<meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">"
			. "<meta charset=""utf-8"" />"
			. "<title>HTMLFile</title>"
			. "</head>"
		. "<body>"
		. "<a href='https://autohotkey.com' data-test='blabla'>AutoHotkey website</a>"
		. "</body>"
		. "</html>"
(oHTML:=ComObjCreate("HTMLFile")).write(html)
e := oHTML.getElementsByTagName("a")[0]
MsgBox % e.getAttribute("hrf") ; no error message with the getAttribute method which simply returns null if the attribute does not exist
MsgBox % e.getAttribute("data-test")
MsgBox % e.getAttribute("href")
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Getting href data, why is it so hard?

13 Dec 2017, 11:38

@A_AhkUser: Thanks so much, I used the getAttribute method just now for the first time, it's made my script so much better.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
A_AhkUser
Posts: 1076
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Getting href data, why is it so hard?

15 Dec 2017, 20:06

jeeswg wrote:@A_AhkUser: Thanks so much, I used the getAttribute method just now for the first time, it's made my script so much better.
I'm not surprised that in the end this doesn't remain a dead letter with the tireless jeeswg. Btw happy 3000th post :happybday: , admirable. :salute: I'm glad it comes in handy.
Cheers

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], BushMange, CEA6597, Google [Bot], VACO BenQ, w0z and 188 guests