代码之家  ›  专栏  ›  技术社区  ›  Brian Bruman

获取dom xpath中数据属性链接的文本值

  •  0
  • Brian Bruman  · 技术社区  · 8 年前

    我尝试过许多XPath表达式、计算、循环等等。 我得到的最好结果是

    " } object(DOMNodeList)#3 (1) { ["length"]=> int(0) }
    

    有人告诉我做错了什么,让我摆脱了痛苦。

    $doc = new DOMDocument;
    libxml_use_internal_errors(true);
    $doc->preserveWhiteSpace = false;
    $doc->strictErrorChecking = false;
    $doc->recover = true;
    $text = urlencode('dog show');
    $html = file_get_contents('https://en.wikipedia.org/w/index.php?search=' . $text . '&title=Special:Search&fulltext=Search');
    $doc->loadHTML(htmlspecialchars($html));
    
    var_dump($doc);
    

    一切都恢复了,没有问题。--

    enter image description here

    现在,我该如何获得 第一 以a href的文本值返回搜索结果 /wiki/Dog_show 或者是 title 或者节点列表中嵌入的跨度值?

    我试过将数据属性作为目标 data-serp-pos="0" 里面有我要找的东西

    $query = "//a/@href[data-serp-pos=\"0\"]";
    $v = $xpath->evaluate($query);
    var_dump($v);
    

    我甚至还试着在Dom树上走得更远

    // $query = '//*[@id="mw-content-text"]/div/ul/li[1]/div[1]/a';
    // $query = '//*[@id="mw-content-text"]/div/ul/li[1]';
    // $query = '//div[@id="mw-content-text"]//a/@href';
    

    尝试循环

    // $result = '';
    // foreach ($xpath->evaluate($query) as $p) {
    //   $result .= $dom->saveHtml($p);
    // }
    // var_dump($result);
    

    添加 string 在评估中, ->nodeValue , ->item(0) 等。

    长度始终为0。

    整个DIV HTML如下所示…

    <div class="mw-search-result-heading"><a href="/wiki/Dog_show" title="Dog show" data-serp-pos="0"><span class="searchmatch">Dog</span><span class="searchmatch">show</span></a></div>
    

    我没有做的(可能很简单)解决方案是什么? href 值和关联的链接文本(或标题属性——在本例中是相同的)

    1 回复  |  直到 8 年前
        1
  •  1
  •   Professor Abronsius    8 年前

    我经常发现使用chrome中的开发人员工具“检查”我希望指向的元素是最容易的,从中可以复制针对特定节点的xpath表达式。这并不总是返回最有用的xpath表达式,但它通常是一个很好的起点——在本例中,我调整了返回的查询并将其添加到类名中。

    希望有帮助

    $term='dog show';
    $url=sprintf('https://en.wikipedia.org/w/index.php?search=%s&title=Special:Search&fulltext=Search', urlencode( $term ) );
    
    
    printf( '<a href="%s" target="_blank">%s</a>', $url, $url );
    
    libxml_use_internal_errors(true);
    $dom=new DOMDocument;
    $dom->recover=true;
    $dom->formatOutput=true;
    $dom->preserveWhiteSpace=true;
    $dom->strictErrorChecking=false;
    
    $dom->loadHTMLFile( $url );
    $xp=new DOMXPath( $dom );
    
    /* possibly the important bit */
    $query='//*[@id="mw-content-text"]/div/ul/li/div[@class="mw-search-result-heading"]/a';
    
    $col=$xp->query( $query );
    
    $html=array();
    
    if( $col && $col->length > 0 ){
        foreach( $col as $node ){
            $html[]=array(
                'title'=>$node->nodeValue,
                'href'=>$node->getAttribute('href')
            );
        }
    }
    
    
    printf('<pre>%s</pre>',print_r($html,true));
    

    将输出:

    https://en.wikipedia.org/w/index.php?search=dog+show&title=Special:Search&fulltext=Search
    Array(
    [0] => Array
        (
            [title] => Dog show
            [href] => /wiki/Dog_show
        )
    
    [1] => Array
        (
            [title] => Show dog
            [href] => /wiki/Show_dog
        )
    
    [2] => Array
        (
            [title] => Westminster Kennel Club Dog Show
            [href] => /wiki/Westminster_Kennel_Club_Dog_Show
        )
    
    [3] => Array
        (
            [title] => Dog Eat Dog (U.S. game show)
            [href] => /wiki/Dog_Eat_Dog_(U.S._game_show)
        )
    
       .......... etc