首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DOMXPath需要帮助

DOMXPath需要帮助
EN

Stack Overflow用户
提问于 2015-04-08 05:42:42
回答 1查看 108关注 0票数 0

我编写了一个代码,搜索特定的文档名(例如: SZ-1000)并收集div class="box"包含的所有链接。(index.php)文档名可能包含一个或两个带有ID的文档。(26904,26905)它的工作方式类似于一个魅力。我把身份证拿回来。

但是我想列出链接作为链接包含的所有附件。诀窍是,现在没有div元素,只有tabledd来指定附件位置。

  • 站点>
    • 文档名(SZ-1000) >
      • 文档ID >
        • 附件链接

我觉得那部分有问题:

$xpath->query('//table[@id="content clear-block"]');

代码语言:javascript
复制
Catchable fatal error: Object of class DOMNodeList could not be converted to string in C:\AppServ\www\test\import.php on line 35

var_dump($articles)在import.php中的应用结果;

代码语言:javascript
复制
object(DOMNodeList)#3 (0) { }
object(DOMNodeList)#3 (0) { }

我的代码:

index.php

代码语言:javascript
复制
$site = 'http://192.168.0.1:81/?q=search/node/SZ-1000';
$html = file_get_contents($site);
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$articles = $xpath->query('//div[@class="box"]');

$links = array();
   foreach($articles as $container) {
   $arr = $container->getElementsByTagName("a");
     foreach($arr as $item) {
      $href =  $item->getAttribute("href");
      $links[] = $href;
     }
}
   foreach($links as $link){
     $text = end(split('/',$value));
     echo $text."<br>";
     $wr_out = file_get_contents("http://127.0.0.1/test/import.php?value=".$text);
     echo $wr_out;
  }

import.php

代码语言:javascript
复制
$value = $_GET['value'];
$site = 'http://192.168.0.1:81/?q=node/'.$value;
$html = file_get_contents($site);
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$articles = $xpath->query('//table[@id="content clear-block"]');

$links = array();
   foreach($articles as $container) {
   $arr = $container->getElementsByTagName("a");
      foreach($arr as $item) {
      $href =  $item->getAttribute("href");
      $links[] = $href;
      echo $href;
     }
}

谢谢你的回复!

编辑:

“可捕获的致命错误: DOMNodeList类的对象无法在第35行的C:\AppServ\www\test\import.php中转换为字符串”

错误修正:

echo $wr_out->tagName;

EN

回答 1

Stack Overflow用户

发布于 2015-04-08 14:43:14

哦,我终于成功了。这是后世的解决办法。用UTF-8字符编码。

index.php

代码语言:javascript
复制
    <?php

//从外部源获取一些变量我在Google电子表格中使用它

代码语言:javascript
复制
    $get = $_GET['get'];
    $site = 'http://192.168.0.1:81/?q=search/node/'.$get;
    $html = file_get_contents($site);

    //libxml_use_internal_errors(true);

    $doc = new DOMDocument();
    $doc->loadHTML($html);

    $xpath = new DOMXpath($doc);
    $articles = $xpath->query('//div[@class="box"]');

    if(!empty($articles)){

    $links = array();
    foreach($articles as $container) {
       $arr = $container->getElementsByTagName("a");
       foreach($arr as $item) {
          $href =  $item->getAttribute("href");
          $links[] = $href;
       }
    }
    $wr_out = "";

    foreach($links as $value){
        $text = end(split('/',$value));
        $wr_out.=file_get_contents("http://127.0.0.1/projekt/search/import.php?value=".$text);

    }

    if(empty($wr_out))
        echo "There is no document with that ID";
        else
    echo $wr_out;
    }
    else
    echo "There is no document with that ID";
    ?>

import.php

代码语言:javascript
复制
    $value = $_GET['value'];
    $site = 'http://192.168.0.1:81/?q=node/'.$value;
    $html = file_get_contents($site);


    //libxml_use_internal_errors(true);

    $doc = new DOMDocument();
    $doc->loadHTML($html);



    $elements = $doc->getElementsByTagName('tbody');
    $table = $elements->item(0);

    $rows = $table->childNodes;

        foreach ($rows as $node) {

          if($node->tagName == "tr"){

            $a = $node->firstChild->firstChild;

             foreach ($a->attributes as $attr) {
                if($attr->nodeName == "href"){
                    $value = $attr->nodeValue;
                    ?>
                        <!doctype html>
                        <head>
                            <title>Search</title>
                          <meta charset="UTF-8">
                        </head>
                        <body>
                            <table align="center">

                                <tr>
                                    <td></td>
                                    <td class="style-1">
                                    <br><h3>
                                    <?=$value?> | <a href='<?=$value?>'>LINK</a></h3><hr>
                                    </td>
                                </tr>
                            </table>
                        </body><?

                }
            }
         }
    }?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29506822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档