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 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 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. 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 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
Archived
This topic is now archived and is closed to further replies.