PHP $ xpath->查询表达式不起作用 [英] PHP $xpath->query expression not working
问题描述
问题1
HTML来源:
< tr>
< td class =abc pqr xyz>我想要捕捉的一些内容< / td>
< / tr>
< tr>
< td class =abc pqr xyz>更多的内容我也想捕捉< / td>
< / tr>
< tr>
< td class =abc pqr xyz>我想捕捉此表格中的所有行< / td>
< / tr>
< tr>
< td class =abc pqr xyz>他们都是口袋妖怪,我想要捕捉< / td>
< / tr>
PHP我试过了:
$ url =http://www.example.com/;
$ b $ opts = array('http'=> array('header'=>User-Agent:MyAgent / 1.0 \r\\\
));
$ context = stream_context_create($ opts);
$ text = file_get_contents($ url,false,$ context);
$ dom = new DOMDocument();
@ $ dom-> loadHTML($ text);
$ xpath = new DOMXPath($ dom);
$ divs = $ xpath-> query('// div / @ class =abc pqr xyz/');
foreach($ divs as $ b){
// echo $ b-> name。'< br />';
print_r($ b);
}
但是没有任何东西可以帮助您查询正确的表达式? 问题2 我得到这个错误: 任何想法,为什么我得到这个警告? 问题3 任何想法如何获取此信息并将其转换为像这样的数组: 你的标记,你试图定位 最有可能与此问题有关: https://bugs.php.net/bug.php?id=61858&edit=1 您可以继续使用xpath来定位所需的值。选择所有 PHP xpath query not working. any idea? Problem # 1 PHP i tried: But nothing came, any help for the right expression for this query? Problem # 2 I got this error: any idea, why i am getting this warning? Problem # 3 any idea how can i get this info and convert it to array like this:
Based on your markup, you're trying to target This is most likely related to this issue:
You could just continue to use xpath to target the desired values. Select all those 这篇关于PHP $ xpath->查询表达式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
i通缉检查我是否获得内容,所以我尝试了这一点并获得了所有的href链接:
$ divs = $ xpath->查询( '// A / @ HREF');
foreach($ divs为$ b){
print_r($ b); //这是行#19
}
DOMAttr对象
警告:print_r():尚未在C:\xampp\htdocs\testing\index中实现.php on line 19
< td colspan =2style =>
< h3>< a href =http://www.example.com/?id=xxtitle =查看更多>我不确定标题< / a>
< / h3>
< div class =blahblah>这仅仅是等等等等< / div>
< / td>
< td colspan =2style =>
< h3>< a href =http://www.example.com/?id=xxtitle =查看更多>我不确定标题< / a>
< small class =comeoneman andwomen>没有鞋< / span>
< / h3>
< div class =blahblah>这仅仅是等等等等< / div>
< / td>
数组(
title =>我不确定标题,
link => http:// www .example.com /?id = xx,
small =>不是鞋子,
blahblah =>这只是等等等等等等)
问题#1
< td>
标签,但是在你的查询中,它是 // div
,这是没有道理的。 Target < td>
':
$ rows = $ xpath - > query('// tr / td [@class =abc pqr xyz]');
foreach($ rows为$ b){
echo $ b-> nodeValue。 <峰; br />;
}
问题2
问题3
< td>
的,然后从这里选择每一个作为上下文节点:
$ data = array();
$ td = $ xpath-> query('// td');
foreach($ td as $ b){
$ data [] = array(
'title'=> $ xpath-> evaluate('string(./ h3 / a) ',$ b),
'link'=> $ xpath-> evaluate('string(./h3/a/ @ href)',$ b),
'small'=> ; trim($ xpath-> evaluate('string(./ h3 / small)',$ b)),
'blahblah'=> trim($ xpath-> evaluate('string(.// div [@ class =blahblah])',$ b)),
);
}
HTML Source:<tr>
<td class="abc pqr xyz">Some contents i want to capture</td>
</tr>
<tr>
<td class="abc pqr xyz">more content i want to capture too</td>
</tr>
<tr>
<td class="abc pqr xyz">all row in this table i want to capture</td>
</tr>
<tr>
<td class="abc pqr xyz">they are all pokemon, i want to capture</td>
</tr>
$url = "http://www.example.com/";
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$text = file_get_contents($url,false,$context);
$dom = new DOMDocument();
@$dom->loadHTML($text);
$xpath = new DOMXPath($dom);
$divs = $xpath->query('//div/@class="abc pqr xyz"/');
foreach($divs as $b){
//echo $b->name.'<br />';
print_r($b);
}
i wanted to check if i am getting content, so i tried this and got all href links:$divs = $xpath->query('//a/@href');
foreach($divs as $b){
print_r($b); // this is line #19
}
DOMAttr Object
Warning: print_r(): Not yet implemented in C:\xampp\htdocs\testing\index.php on line 19
<td colspan="2" style="">
<h3><a href="http://www.example.com/?id=xx" title="View more">I am not sure about the title</a>
<small class="comeoneman andwomen">Not a shoe</span>
</h3>
<div class="blahblah">This is just blah blah blah</div>
</td>
<td colspan="2" style="">
<h3><a href="http://www.example.com/?id=xx" title="View more">I am not sure about the title</a>
<small class="comeoneman andwomen">No a shoe</span>
</h3>
<div class="blahblah">This is just blah blah blah</div>
</td>
array (
title => I am not sure about the title,
link => http://www.example.com/?id=xx,
small => not a shoe,
blahblah => This is just blah blah blah
)
Problem #1
<td>
tags, but in your query, it's //div
, which doesn't make sense. Target <td>
's:$rows = $xpath->query('//tr/td[@class = "abc pqr xyz"]');
foreach($rows as $b){
echo $b->nodeValue . '<br/>';
}
Problem #2
Problem #3
<td>
's and from there, just use each of them as the context node:$data = array();
$td = $xpath->query('//td');
foreach($td as $b){
$data[] = array(
'title' => $xpath->evaluate('string(./h3/a)', $b),
'link' => $xpath->evaluate('string(./h3/a/@href)', $b),
'small' => trim($xpath->evaluate('string(./h3/small)', $b)),
'blahblah' => trim($xpath->evaluate('string(./div[@class="blahblah"])', $b)),
);
}