开发者

scraping javascript array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

I had a problem with scraping a site http://www.weather.bm/radarMobile.asp, Fatherstorm gave me a great solution but it had some minor bugs in regards to start time and number of images source sometimes gives 9 or 10 but the code was set to ten.

On the other hand one person (marcog) gave me this answer:

"A simple search through for the line containing radarFileNames = new Array will return this line. Then you can extract the URLs with two preg_split()s: first on \(|\), then split the second token on ', '. Finally, prefix http://www.weather.bm/ to the URLs."

I was hoping if someone can show me the code he was talking about. I like to learn this kind of stuff but I just don't have someone to teach me. I hope you guys could help

javascript array:

radarFileNames = new Arra(     
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1446.jpg',      
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1456.jpg',      
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1506.jpg',      
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1516.jpg',      
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1526.jpg',      
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1536.jpg',      
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1546.jpg',      
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1556.jpg',      
'images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-2011-01-04-1606.jpg'

also would it be possible to output the img inside ul li /li /ul which has it own class (ul class li class)

Im also using simple html dom parser but I can't get the images only the loaded images is outputed

here is the sample using fatherstorm code http://j2sdesign.com/rgw/article/20101222/NEWS01/712229951/0/example/r1.php

my code from fatherstorm

?php
        $localOffset = "+2 Hours"; //change this to your local offset from the image times you need. for me  it's 1 hours.....
        $start = strtotime("-100 Minutes " . $localOffset); // go back 90 minutes (The limit of the available images)
        $start = ($start - ($start % (600)) + (60 * 6)); //go to the next earlier 6 minute mark (all images appear to be at the 6 minute mark))
        for ($x = 1; $x <= 10; $x++) {
            $DateTime = date("Y-m-d-Hi", $start); // set the image time we want.
            $fName开发者_如何学Python = ("http://www.weather.bm/images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-$DateTime.jpg"); // set the filename
            echo" <center>$DateTime</center> <img class='radarImg' src='$fName' alt='$fName'  title='$fName'/><br/."; // echo (or add to a stack or variable...) your image node
            $start+= ( 600); //add 10 minutes


        }
        ?

mario do you mean like this:

<?php
        $localOffset = "+2 Hours"; //change this to your local offset from the image times you need. for me  it's 1 hours.....
        $start = strtotime("-100 Minutes " . $localOffset); // go back 90 minutes (The limit of the available images)
        $start = ($start - ($start % (600)) + (60 * 6)); //go to the next earlier 6 minute mark (all images appear to be at the 6 minute mark))
        for ($x = 1; $x <= 10; $x++) {
            $DateTime = date("Y-m-d-Hi", $start); // set the image time we want.
            $fName = ("http://www.weather.bm/images/Radar/CurrentRadarAnimation_100km_sri/100km_sri-radar-$DateTime.jpg"); // set the filename
            echo" <center>$DateTime</center> <img class='radarImg' src='$fName' alt='$fName'  title='$fName'/><br/."; // echo (or add to a stack or variable...) your image node
            $start+= ( 600); //add 10 minutes
        $html = file_get_contents('http://www.weather.bm/radarMobile.asp'); preg_match('/radarFileNames = new Array\((.+?)\);/ims', $html, $m); $files = explode(",", $m[1]);

        }
        ?>


Read the PHP manual on regular expressions. In this case it's as simple as:

$html = file_get_contents('http://www.weather.bm/radarMobile.asp');
preg_match('/radarFileNames = new Array\((.+?)\);/ims', $html, $m);
$files = explode(",", $m[1]);
// then output <img>s

The explode() is easier here than preg_split. But then requires a trim() on each filename in the array, and a second trim($filename, "'") to get rid of the enclosing quotes.


Try this:

//not tested
var len=radarFileNames.length, links='';

for(var i=0; i<len; i++) {
    var links = links + '<a href="radarFileNames[i]">'+radarFileNames[i] + '</a>';
}

document.getElementById("some_id").innerHTML = links;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜