0
votes

Im trying to extract lots of information from a website, and Im unfamiliar with the syntax I should use to get specific content, I've tried reading up on RegEx and match API for actionscript 3, but Im still unsure.

This is my code:

var l1:URLLoader = new URLLoader();
l1.addEventListener(Event.COMPLETE, completeHandler);
l1.load(new URLRequest("https://meny.no/oppskrifter/Pasta/baked-feta-pasta/"));
trace("load");

function completeHandler(e:Event):void {
    trace("complete")
    var s:String = e.target.data;
    //var targets:Array = s.match(/(?<=<div class="target">).*(?=<\/div>)/igm);
    trace(targets);
    //getting the name of the recipe
    var targets:Array = s.match(/(?<=<h1 class="c-h1">).*(?=<\/h1>)/igm);
    trace(targets);
    // getting the ingress of the recipe
    targets[1] = s.match(/(?<=<div class="c-recipe__intro">).*(?=<\/div>)/sigm);
    trace(targets[1]);
    trace("complete2");
}

What I'm trying to grep with this line:
targets[1] = s.match(/(?<=).*(?=</div>)/sigm);

Is getting this information only: Oppskrift på TikTok trenden "Baked feta pasta", en enkel pasta med saus av ovnsbakt fetaost og tomater. Retten er enkel med få ingredienser og mye smak. Fetaost, tomater, olivenolje og urter ovnsbakes, og blandes så med kokt pasta.

But instead it gives me everything after aswell Anyway, is there a template or something that explains how to get certain information in a more graspable way? Thanks!

Its similar to this question: But not quite the same

In swf AS3, how do you extract string content from a website

1
Question №1: is there a point to do it with RegEx? It might be easier to compose your own string search method, something like findAll(text:String, head:String, tail:String):Array than to RegEx it. You might also want to read why parsing HTML with RegEx is not a good idea in general: blog.codinghorror.com/parsing-html-the-cthulhu-way Question №2: why AS3? - Organis
I didnt know there was another way. Im using as3 in air for android - Oliver

1 Answers

0
votes

There of course is another (rather than RegEx) approach, an algorithmic one.

Something like that will suffice for searching many sub-strings by the given head and tail:

function findMany(text:String, head:String, tail:String):Array
{
    var result:Array = new Array;
    
    // At this point we get several slices, each ends with the provided "tail".
    var aList:Array = text.split(tail);
    
    // The last chunk doesn't end with "tail".
    aList.pop();
    
    // Iterate over them.
    for each (var a:String in aList)
    {
        // Find out where (and if) the "head" in the each slice.
        // If there's more than one, take the last.
        var anIndex:int = a.lastIndexOf(head);
        
        if (anIndex > -1)
        {
            // If there is one, add it to the list of results.
            // Don't forget the "tail" as .split() method
            // cuts the given separator.
            result.push(a.substr(anIndex) + tail);
        }
    }
    
    return result;
}