Copy link to clipboard
Copied
Hi guys,
Ok...so I have figured out how to visit a URL and after that URL has loaded, get a HTTP status back to know if I visited a valid web page or not. However, now I am needing to know a simply way of basically storing all the hyperlinks that may exist on that page into an array/string.
I'm in a sense trying to scan the entire webpage I just loaded and grab anything thing on that page that is a hyperlink and store it in an array for use elsewhere in my actionscript program. Keep in mind, I'm visiting the website behind the scenes in an Adobe Air program.
Copy link to clipboard
Copied
Not sure if this is the fastest or best way to do it, but using a while loop and String methods, you can extract links from the HTML if you can provide it as a String. The following code first scans the String variable, which should be your HTML source, for href links that use single quotes around the links. The second scan searches the HTML source for href links with double quotes. It scans the entire source String until no more links can be found and records the links it does find in a Vector.<String> array.
var htmlSource:String = "";
htmlSource += "<a href='http://www.google.com/'>google</a>";
htmlSource += "<a href=\"http://www.yahoo.com/\">yahoo</a>";
htmlSource += "<a href='http://www.bing.com/'>bing</a>";
htmlSource += "<a href=\"http://www.adobe.com/\">adobe.com</a>";
var index:int = 0;
var links:Vector.<String> = new Vector.<String>();
while (htmlSource.indexOf("href='", index) != -1) {// while a href link with single quotes can be found, do the following
index = htmlSource.indexOf("href='", index) + 6;// record index position plus 6 characters to get the index to be where the true link starts
// add to array the String that gets created from the HTML source
// String.substring() returns a String starting at the provided index value up to the end index -1.
// end index is generated by searching for the index of the ending single quote of the href link starting with beginning index
links.push(htmlSource.substring(index, htmlSource.indexOf("'", index)));
}
index = 0;
// perform same search as above, except with double quotes
while (htmlSource.indexOf("href=\"", index) != -1) {// while a href link with double quotes can be found, do the following
index = htmlSource.indexOf("href=\"", index) + 6;
links.push(htmlSource.substring(index, htmlSource.indexOf("\"", index)));
}
trace(links);