Seminko

Getting a specific code from a webpage.

19 posts in this topic

#1 ·  Posted (edited)

Hey fellas,

I need to extract a line of text from the code of this page: http://www.hearthhead.com/card=374/ragnaros-the-firelord

What I'm looking for is this piece of code which contains qoutes for the related card, which is tagged as Tooltip.

//<![CDATA[
function playCardSound(id) { var aud = $WH.ge('cardsound'+id); aud.play(); }$WH.aE(window, 'load', function() { var shown = 0; 
var a = $WH.ge('cardsoundlink0'), audio=$WH.ge('cardsound0');
$WH.Tooltip.simple(a, "<i>By fire be purged.</i>", null, true);

if (('networkState' in audio) && (audio.networkState != 3)) shown++; 
    else a.style.display='none';
var a = $WH.ge('cardsoundlink1'), audio=$WH.ge('cardsound1');
if (('networkState' in audio) && (audio.networkState != 3)) shown++; 
    else a.style.display='none';
var a = $WH.ge('cardsoundlink2'), audio=$WH.ge('cardsound2');
$WH.Tooltip.simple(a, "<i>Too soon.</i>", null, true);

if (('networkState' in audio) && (audio.networkState != 3)) shown++; 
    else a.style.display='none';
var a = $WH.ge('cardsoundlink3'), audio=$WH.ge('cardsound3');
if (('networkState' in audio) && (audio.networkState != 3)) shown++; 
    else a.style.display='none';
if (shown > 0) $WH.ge('sounds').style.display='inline-block';});
//]]>

In this case I want to extract:

  • By fire be purged.
  • Too soon.

 

I tried this but it is ridiculously slow - takes maybe a minute.

#include <IE.au3>
#include <MsgBoxConstants.au3>

Local $oIE = _IECreate("http://www.hearthhead.com/card=374")
Local $sText = _IEBodyReadText($oIE)
;MsgBox($MB_SYSTEMMODAL, "Body Text", $sText)
FileWrite("***.txt", $sText)

_IEQuit($oIE)

 

Is there a way to make this process quicker? The idea behind this is as follows. There are over 2500 cards, each of them on their own page, and I want each of them scanned and the qoutes extracted.

 

Thanks

Seminko

Edited by Seminko

Share this post


Link to post
Share on other sites



probably you can walk thru the script tags

for example type in addressbar

javascript:for (x in document.scripts){if (document.scripts[x].text.indexOf("playCardSound")>0){alert(document.scripts[x].text);};};void(0);

no clue how to get there with IE.UDF but with JavaScript loop it should not be that hard to get it from the popup

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

 

#Include <Array.au3>
$source = BinaryToString(InetRead("http://www.hearthhead.com/card=374/ragnaros-the-firelord"))
$res = StringRegExp($source, '(?s)(?:cardsoundlink.*?<i>([^<]+))+', 3)
 _ArrayDisplay($res)

 

 As it turns out, this method is quite fast. Is it because the target is downloaded, which takes significantly less time compared to the time it takes for the site to be actually loaded in a browser?

Thanks Mikell

Edited by Seminko

Share this post


Link to post
Share on other sites

 As it turns out, this method is quite fast. Is it because the target is downloaded, which takes significantly less time compared to the time it takes for the site to be actually loaded in a browser?

Yes    :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Yes    :)

EDIT: I would also like the card title to be displayed. In the code it looks like this:

<title>Ragnaros the Firelord - Card - Hearthstone</title>

All I want is "Ragnaros the Firelord". I know how to do it non-regexp way (look for "title", capture position 1, look for " - Card", capture positon 2, string mid) but since we're using RegExp I would appreciate if you could help me with that and also explain what parameters and why you used. It seems reading the function page is just not enough for me to grasp it. Thx

Edited by Seminko

Share this post


Link to post
Share on other sites

OK   :)

#Include <Array.au3>
$source = BinaryToString(InetRead("http://www.hearthhead.com/card=374/ragnaros-the-firelord"))
$res = StringRegExp($source, '(?s)^|(?:cardsoundlink.*?<i>([^<]+))+', 3)
$res[0] = StringRegExpReplace($source, '(?s).*?<title>(.*?)\h*-\h*Card.*', "$1")
 _ArrayDisplay($res)

Using  ^|  in the first regex is a way to ensure that index [0] in the resulting array $res is set aside and empty
Then this $res[0] is filled with the result of the SREReplace
The srer is easy to understand : "keep all chars (lazy) after the first '<title>' found, up to a sequence 'space(s)-hyphen-space(s)-Card' "

1 person likes this

Share this post


Link to post
Share on other sites

Thanks fellas!

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

OK   :)

$res[0] = StringRegExpReplace($source, '(?s).*?<title>(.*?)\h*-\h*Card.*', "$1")

The srer is easy to understand : "keep all chars (lazy) after the first '<title>' found, up to a sequence 'space(s)-hyphen-space(s)-Card' "

 

Ok, another example. In the string from InetRead there are links like this:

<source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Play_01.ogg" type="audio/ogg; codecs=&quot;vorbis&quot;">
<source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_134_Attack_02.mp3" type="audio/mpeg">

I want to be able to extract only the file links but only the ones with mp3:

//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_134_Attack_02.mp3

 

So I used your example as a starting point and it extracts all the links correctly however both *.OGG and *.MP3 are included:

StringRegExp($source, '(?s).*?<source src="(.*?)"*\h*type*', 3)

So now we have:

Keep all chars after the first ' <source src=" ', up to a sequence 'space(s)-hyphen-type

 

However I cannot find a way to do:

Keep all chars after the first ' <source src=" ', up to a sequence 'space(s)-hyphen-type only if there is a sequence of "mp3" in the middle.

 

Again, if you could explain in the simplest terms it would help me a lot. Thanks

Edited by Seminko

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Here is an example
The easier way is to choose 'mp3' as end mark for the part to match, while including it in the capture group

$s = '<source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Play_01.ogg" type="audio/ogg; codecs=&quot;vorbis&quot;">' & @crlf & _
'<source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_134_Attack_02.mp3" type="audio/mpeg">'

$res = StringRegExp($s, '(?s)<source src="(.*?mp3)', 3)
msgbox(0,"", $res[0])

=> Keep all chars after a ' <source src=" ', up to a sequence 'mp3', included
If you don't make the .*? lazy, you will get all chars between the first <source src=" and 'mp3'

Edited by mikell
1 person likes this

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Here is an example
The easier way is to choose 'mp3' as end mark for the part to match, while including it in the capture group

$s = '<source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Play_01.ogg" type="audio/ogg; codecs=&quot;vorbis&quot;">' & @crlf & _
'<source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_134_Attack_02.mp3" type="audio/mpeg">'

$res = StringRegExp($s, '(?s)<source src="(.*?mp3)', 3)
msgbox(0,"", $res[0])

=> Keep all chars after a ' <source src=" ', up to a sequence 'mp3', included
If you don't make the .*? lazy, you will get all chars between the first <source src=" and 'mp3'

That returns this:

//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Play_01.ogg" type="audio/ogg; codecs=&quot;vorbis&quot;"><source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Play_01.mp3
//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Trigger_03.ogg" type="audio/ogg; codecs=&quot;vorbis&quot;"><source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Trigger_03.mp3
//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Attack_02.ogg" type="audio/ogg; codecs=&quot;vorbis&quot;"><source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Attack_02.mp3
//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Death_04.ogg" type="audio/ogg; codecs=&quot;vorbis&quot;"><source src="//wowimg.zamimg.com/hearthhead/sounds/VO_EX1_298_Death_04.mp3

There is no @CRLF in the code so that might be the issue?

Edited by Seminko

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Hum sorry I was not awake enough...
The regex needs to be more selective "keep all non-quote chars..."

'<source src="([^"]+mp3)'

It can be even more selective using

'<source src="([/\w.]+mp3)'

= Keep all chars which are only a slash, or a dot, or a word char up to 'mp3' included

Edited by mikell
1 person likes this

Share this post


Link to post
Share on other sites

Hum sorry I was not awake enough...
The regex needs to be more selective "keep all non-quote chars..."

'<source src="([^"]+mp3)'

That works. But I don't get it :-D

So we used:

StringRegExp($s, '(?s)<source src="(.*?mp3)', 3)

Which is ' Keep all chars after a ' <source src=" ', up to a sequence 'mp3', included '. And it picked  ' <source src=" ' that was related to the OGG link.

I kinda don't get the fix you posted. So it again finds the ' <source src=" ' that is related to the OGG. But what we told RegExp with the line "keep all non-qoute chars" ? That it should find a string between ' <source src=" ' and ' mp3 ' but i cannot include qoutes? So since there are qoutes between the OGG related ' <source src=" ' and ' mp3 ' it knew it had to jump to the mp3 related  ' <source src=" ' which is the closest to the mp3 and there are no qoutes between those ?

Why didn't it pick the one related to the MP3 link in the first place? Isn't there a setting to say sth like: find ' <source src=" ' that is the closest to ' mp3 ' but before it?

Share this post


Link to post
Share on other sites

The dot means 'any char', so the regex engine begins to read the text, finds the first <source src=", then grabs all - including the next <source src=" which can be matched by the dot - up to 'mp3'
Being selective using [^"]  make it fail when after <source src=" it encounters a quote, so it captures nothing and then goes on, finds the next <source src=" etc

1 person likes this

Share this post


Link to post
Share on other sites

Right, understood this is the easiest solution in this particular case.

 

Thanks Mikell!

Share this post


Link to post
Share on other sites

We must never forget that the regex engine acts like most of us, reading from left to right and doing precisely what we asked it to do, using pure logic - reason why we need to be very accurate when building a pattern  :D

Share this post


Link to post
Share on other sites

the regex engine acts like most of us, reading from left to right and doing precisely what we asked it to do, using pure logic

I suspect a significant number of exceptions.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Hmm I certainly didn't phrase it correctly, I meant
the regex engine acts like most of us (it reads from left to right), it does precisely what we asked it to do and it uses pure logic
;)

Share this post


Link to post
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