dumb2champ Posted March 16, 2015 Share Posted March 16, 2015 Please help regarding scraping website using Xpath...I want to scrap the main content of a website..but theres a problem...the scripts does not return all content but only the first line of content at main body of website...Scripts like below.... <?php //sentimen order if (PHP_SAPI != 'cli') { echo "<pre>"; } require_once __DIR__ . '/../autoload.php'; $sentiment = new \PHPInsight\Sentiment(); require_once 'Xpath.php'; //my target website $startUrl = "http://news.sky.com/story/1445575/suspect-held-over-shooting-of-ferguson-police/"; $xpath = new XPATH($startUrl); // my specific filter $query = '/html/body/div[2]/div[3]/article/div/div[2]/div[2]'; //start xpath $strQuery = $xpath->query($query); $strNode = $strQuery->item(0)->nodeValue; //THE PROBLEM START HERE..ARRAY SCRIPTS SEEMS WRONG.... $result = array($strNode); //HERE ALSO foreach ($result as $string) { // calculations: $scores = $sentiment->score($string); $class = $sentiment->categorise($string); // output: echo "Strings $string \n"; echo "Dominant: $class, scores: "; print_r($scores); echo "\n"; } The scripts output only single line of sentiment but not all the required content... The problem come from array and foreach...Please help to repair these scripts...Thanks Quote Link to comment https://forums.phpfreaks.com/topic/295276-xpath-does-not-return-all-content/ Share on other sites More sharing options...
CroNiX Posted March 17, 2015 Share Posted March 17, 2015 (edited) Look at the previous line. $strNode = $strQuery->item(0)->nodeValue;You are only grabbing the first element's node value, and then you force that to be an array. So it's an array with one value. Edited March 17, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/295276-xpath-does-not-return-all-content/#findComment-1508270 Share on other sites More sharing options...
dumb2champ Posted March 17, 2015 Author Share Posted March 17, 2015 thanks...i try it...nice pic Quote Link to comment https://forums.phpfreaks.com/topic/295276-xpath-does-not-return-all-content/#findComment-1508277 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.