PHP $ xpath->查询表达式不起作用 [英] PHP $xpath->query expression not working

查看:100
本文介绍了PHP $ xpath->查询表达式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP xpath查询不起作用。任何想法?

问题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

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

任何想法,为什么我得到这个警告?






问题3


 < td colspan =2style => 
< h3>< a href =http://www.example.com/?id=xxtitle =查看更多>我不确定标题&l​​t; / a>


< / h3>

< div class =blahblah>这仅仅是等等等等< / div>

< / td>
< td colspan =2style =>
< h3>< a href =http://www.example.com/?id=xxtitle =查看更多>我不确定标题&l​​t; / 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



最有可能与此问题有关:


https://bugs.php.net/bug.php?id=61858&edit=1




问题3



您可以继续使用xpath来定位所需的值。选择所有< 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)),
);
}

样本输出


PHP xpath query not working. any idea?

Problem # 1
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>

PHP i tried:

$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);
}

But nothing came, any help for the right expression for this query?


Problem # 2
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
}

I got this error:

DOMAttr Object
Warning: print_r(): Not yet implemented in C:\xampp\htdocs\testing\index.php on line 19

any idea, why i am getting this warning?


Problem # 3

                    <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>

any idea how can i get this info and convert it to array like this:

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

Based on your markup, you're trying to target <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/>';
}

Sample Output

Problem #2

This is most likely related to this issue:

https://bugs.php.net/bug.php?id=61858&edit=1

Problem #3

You could just continue to use xpath to target the desired values. Select all those <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)),
    );
}

Sample Output

这篇关于PHP $ xpath-&gt;查询表达式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆