Jump to content

XPath does not return all content


dumb2champ

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.