Grabbing text from a document Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
diaewad
Posts: 101
Joined: 17 Feb 2015, 10:55

Grabbing text from a document

25 Mar 2015, 14:00

So I've scoured the seven seas and cannot figure this out.

Let's say I have a string of 10,000 characters. In that string there is a section I want to extrapolate; however, I don't know what it is. I only know what comes before it. Is there a way to look for a string and then copy the 10 characters after that string? For instance...

lkjsdflkjsdflkjsdflkjsdflkjdfslollerskates:roflcopter

I would find "lollerskates" and then want the "roflctoper" portion after it. The "lollerskates" part is always the same, but the characters after it change, and that's the info I want.
Last edited by diaewad on 25 Mar 2015, 16:04, edited 1 time in total.
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Grabbing text from a document

25 Mar 2015, 14:55

You could try RegEx

Code: Select all

str = lkjsdflkjsdflkjsdflkjsdflkjdfslollerskates:roflcopter

find = lollerskates	; initial search string
chrs = 10 			; amount of characters to search forward

RegExMatch( str, "s)" find ":(?<match>.{0," chrs "})", _ )

msgbox % _match
diaewad
Posts: 101
Joined: 17 Feb 2015, 10:55

Re: Grabbing text from a document

25 Mar 2015, 15:03

I've never used RegExMatch before, so I can't seem to understand the variables used here and how to incorporate them into my script. What I need is to grab the next 4 characters after '<span id="yfs_l84_jtpy">' (reverse quoted for easier readability). I tried swapping out what you have, but it returned blank.

Code: Select all

find = <span id="yfs_l84_jtpy"> ; initial search string
chrs = 4          ; amount of characters to search forward

RegExMatch( str, "s)" find ":(?<match>.{0," chrs "})", _ )

msgbox % _match
diaewad
Posts: 101
Joined: 17 Feb 2015, 10:55

Re: Grabbing text from a document

25 Mar 2015, 15:07

Nevermind! I got it. Took out the colon, and it worked. Thanks so much!
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Grabbing text from a document

25 Mar 2015, 15:15

If this is html, you're better off using an html object to get the data.
For instance

Code: Select all

html = <span id="yfs_l84_jtpy">four</span>

HTMLObj := ComObjCreate( "HTMLfile" ), HTMLObj.write( html )

msgbox % HTMLObj.getElementsByTagName( "span" ).Item( 0 ).innerText
It's easier to help if you let us know exactly what you're after in the first place.
diaewad
Posts: 101
Joined: 17 Feb 2015, 10:55

Re: Grabbing text from a document

25 Mar 2015, 15:22

Oh, I see. I thought it would be simple. Which... I guess it may be for you.

I'm trying to create an extremely minimal stock ticker for my desktop. The GUI and everything works fine, but I can't seem to get the stock price to show up. Yahoo! Finance just stopped their csv stock feed, so I'm trying to do a UrlDownloadToFile on their site and grab the stock price through their html. For instance:

Code: Select all

StockName := "AAPL"
URLDownloadToFile, http://finance.yahoo.com/q?uhb=uh3_finance_vert&fr=&type=2button&s=%StockName%, C:\Stock.txt
Then use the script you gave me to extrapolate the price that follows '<span id="yfs_l84_%stockname%">' in the file. Although that doesn't seem to work with a variable for some reason.
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Grabbing text from a document

25 Mar 2015, 15:38

Try this function in your code

Code: Select all

Msgbox % GetStockPrice( "AAPL" )

GetStockPrice( Sym )
{
    URL = http://finance.yahoo.com/q?uhb=uh3_finance_vert&fr=&type=2button&s=
    ReqObj := ComObjCreate( "WinHttp.WinHttpRequest.5.1" ), HTMLObj := ComObjCreate( "HTMLfile" )
    ReqObj.Open( "GET", URL . Sym, false ), ReqObj.Send(), HTMLObj.write( ReqObj.ResponseText )

    While ( SpanObj := HTMLObj.getElementsByTagName( "span" ) ).Item( a_index-1 ).id != "yfs_l84_" Sym
        Price := SpanObj.Item( a_index-1 ).InnerText

    Return Price
}
diaewad
Posts: 101
Joined: 17 Feb 2015, 10:55

Re: Grabbing text from a document

25 Mar 2015, 15:47

That works! You're a genius! Thanks!
ineed2know

Re: [SOLVED] Grabbing text from a document

22 Jun 2016, 10:37

unfortunatelly the code to grab a stock price no longer works:
Spoiler
any idea why? I get an error regarding the server connection.
diaewad
Posts: 101
Joined: 17 Feb 2015, 10:55

Re: [SOLVED] Grabbing text from a document

22 Mar 2017, 14:56

A little late in answering you, but I'm pretty sure it's because that URL is no longer valid. They've changed their site.
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: [SOLVED] Grabbing text from a document

22 Mar 2017, 22:24

this fixes the issue

Code: Select all

msgbox % GetStockPrice( "AAPL" )

GetStockPrice( Sym )
{
    URL = http://finance.yahoo.com/quote/
    ReqObj := ComObjCreate( "WinHttp.WinHttpRequest.5.1" ), HTMLObj := ComObjCreate( "HTMLfile" )
    ReqObj.Open( "GET", URL . Sym, false ), ReqObj.Send(), HTMLObj.write( ReqObj.ResponseText )

	QtSpObj := HTMLObj.getElementById( "quote-header-info" ).getElementsByTagName( "span" )

	While !( ( Price := QtSpObj[ a_index-1 ].innerText ) ~= "^\d{1,3}[\.\d]+$" )
		Continue

    Return Price
}
As a learning challenge, add error handling as it's needed.
alnz123
Posts: 13
Joined: 31 Jan 2021, 22:01

Re: [SOLVED] Grabbing text from a document

13 Jan 2022, 22:26

Hi
Thanks for this TLM - it was working for a while but recently stopped working. Yahoo must have changed their code but I don't fully understand your script so can't get it working again.
Appreciate if anyone can help.
Thanks
TLM wrote:
22 Mar 2017, 22:24
this fixes the issue

Code: Select all

msgbox % GetStockPrice( "AAPL" )

GetStockPrice( Sym )
{
    URL = http://finance.yahoo.com/quote/
    ReqObj := ComObjCreate( "WinHttp.WinHttpRequest.5.1" ), HTMLObj := ComObjCreate( "HTMLfile" )
    ReqObj.Open( "GET", URL . Sym, false ), ReqObj.Send(), HTMLObj.write( ReqObj.ResponseText )

	QtSpObj := HTMLObj.getElementById( "quote-header-info" ).getElementsByTagName( "span" )

	While !( ( Price := QtSpObj[ a_index-1 ].innerText ) ~= "^\d{1,3}[\.\d]+$" )
		Continue

    Return Price
}
As a learning challenge, add error handling as it's needed.
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: [SOLVED] Grabbing text from a document  Topic is solved

14 Jan 2022, 00:30

Try this:

Code: Select all

msgbox % GetStockPrice( "AAPL" )

GetStockPrice( Sym )
{
    URL = http://finance.yahoo.com/quote/
    ReqObj := ComObjCreate( "WinHttp.WinHttpRequest.5.1" ), HTMLObj := ComObjCreate( "HTMLfile" )
    ReqObj.Open( "GET", URL . Sym, false ), ReqObj.Send(), HTMLObj.write( ReqObj.ResponseText )

	QtSpObj := HTMLObj.getElementByID( "quote-header-info" ).getElementsByTagName( "fin-streamer" )

	While !( ( Price := QtSpObj[ a_index-1 ].value ) ~= "^\d{1,3}[\.\d]+$" )
		Continue

    Return Price
}
alnz123
Posts: 13
Joined: 31 Jan 2021, 22:01

Re: [SOLVED] Grabbing text from a document

14 Jan 2022, 01:48

:clap:
THANK YOU!

it was the "value" on line 11 the fixed it, thanks. (I changed line 9 to "fin-streamer" but keep getting an error.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 378 guests