0

I am trying to retrieve content from a p element in this page. As you can see, in the source code there is a paragraph with the content i want:

<p id="qb"><!--
QBlastInfoBegin
    Status=READY
QBlastInfoEnd
--></p>

Actually i want to take the value of the Status. Here is my PHP code.

@$dom->loadHTML($ncbi->ncbi_request($params));
$XPath = new DOMXpath($dom);
$nodes = $XPath->query('//p[@id="qb"]');
$node  = $nodes->item(0)->nodeValue;
var_dump($node))

that returns

["nodeValue"]=> string(0) ""

Any idea ?

Thanks!

Community
  • 1
  • 1
F.N
  • 401
  • 4
  • 21

2 Answers2

2

Seems that to get comment values you need to use //comment() I'm not too familiar with XPaths so am not too sure on the exact syntax

Sources: https://stackoverflow.com/a/7548089/723139 / https://stackoverflow.com/a/1987555/723139

Update: with working code

<?php

$data = file_get_contents('http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?RID=UY5PPBRH014&CMD=Get');
$dom = new DOMDocument();
@$dom->loadHTML($data);
$XPath = new DOMXpath($dom);
$nodes = $XPath->query('//p[@id="qb"]/comment()');
foreach ($nodes as $comment)
{
    var_dump($comment->textContent);
}
Community
  • 1
  • 1
thewheat
  • 988
  • 7
  • 13
  • 1
    This will work: `$nodes = $XPath->query('//p[@id="qb"]/comment()');` if you want to update your answer. – James Jun 29 '14 at 12:50
1

I checked up the site, and it seems you are after the comment inside, you need to add comment() on your xpath query. Consider this example:

$contents = file_get_contents('http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?RID=UY5PPBRH014&CMD=Get');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($contents);
libxml_clear_errors();
$xpath = new DOMXpath($dom);

$comment = $xpath->query('//p[@id="qb"]/comment()')->item(0)->nodeValue;
echo '<pre>';
print_r($comment);

Outputs:

QBlastInfoBegin
    Status=READY
QBlastInfoEnd
user1978142
  • 7,946
  • 3
  • 17
  • 20