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
Share on other sites

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 by CroNiX
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.