Natefons Posted October 10, 2009 Share Posted October 10, 2009 background im trying to design a page that uses data from another website (lets say http://example.com) the said website doesnt have any sort of API, but they do output all there data to the Source. (view->source) Question: soo im wondering, what would be the ideal method to parse data from this website and get the data values i need? i know, File_get_contents(URL) will grab the source from said url, but now how can i grab that data? suppose file_get_contents grabs this: <title>this is my title</title> <body> blah blah blah <li class="points"> <span>Points</span> <strong>2370</strong> </li> </body> now i want to display the number of points on MY page..how can i go about? --hope i was clear enough Nate Quote Link to comment https://forums.phpfreaks.com/topic/177225-website-parsing/ Share on other sites More sharing options...
newbtophp Posted October 10, 2009 Share Posted October 10, 2009 <?php $content = file_get_contents('http://www.website.com/page.php'); $temp = explode('<strong>',$content); $temp2 = explode('</strong>',$temp[1]); //Get the points $points = $temp2[0]; unset($content,$temp,$temp2); print_r($points); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177225-website-parsing/#findComment-934462 Share on other sites More sharing options...
MadTechie Posted October 10, 2009 Share Posted October 10, 2009 preg_match('%<li class="points">.*?<strong>(\d+)</strong>\s*</li>%s', file_get_contents('http://www.website.com/page.php'), $regs)); echo $regs[1]; Quote Link to comment https://forums.phpfreaks.com/topic/177225-website-parsing/#findComment-934472 Share on other sites More sharing options...
Natefons Posted October 10, 2009 Author Share Posted October 10, 2009 can both of you kindly explain what each script does? thanks -Nate Quote Link to comment https://forums.phpfreaks.com/topic/177225-website-parsing/#findComment-934508 Share on other sites More sharing options...
newbtophp Posted October 10, 2009 Share Posted October 10, 2009 <?php //Here we use file_get_contents to get the page without executing it yet $content = file_get_contents('http://www.website.com/page.php'); //Then we grab the first html tag $temp = explode('<strong>',$content); //Next we grab the end html tag $temp2 = explode('</strong>',$temp[1]); //Then we grab the points which is contained within the above tags $points = $temp2[0]; //Then we combine all the variables unset($content,$temp,$temp2); // Now we are done and display it print_r($points); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177225-website-parsing/#findComment-934511 Share on other sites More sharing options...
Natefons Posted October 10, 2009 Author Share Posted October 10, 2009 mad techie, your script gives me a parse error and newb, why are you combining the variables again? since the output (seems) independent of the unset Quote Link to comment https://forums.phpfreaks.com/topic/177225-website-parsing/#findComment-934523 Share on other sites More sharing options...
mikesta707 Posted October 10, 2009 Share Posted October 10, 2009 phpnewb's example will only work if AND ONLY IF the format of the page is exactly the same of your example. What if there are strong tags before the points value? what if there are <b> tags? madtechies is better, but it also assumes that there is <strong>points</strong> are you sure that the format for the page(s) will have <strong>points</strong>. could it be different? if not i suggest using madtechies example, but if so, then more info is needed Quote Link to comment https://forums.phpfreaks.com/topic/177225-website-parsing/#findComment-934527 Share on other sites More sharing options...
.josh Posted October 10, 2009 Share Posted October 10, 2009 background the said website doesnt have any sort of API, but they do output all there data to the Source. (view->source) stealing other people's content makes pandas sad. anyways, I suggest you look into DOM Quote Link to comment https://forums.phpfreaks.com/topic/177225-website-parsing/#findComment-934541 Share on other sites More sharing options...
MadTechie Posted October 10, 2009 Share Posted October 10, 2009 more info is needed I Agree stealing other people's content makes pandas sad. I Agree anyways, I suggest you look into DOM I Agree Quote Link to comment https://forums.phpfreaks.com/topic/177225-website-parsing/#findComment-934570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.