【问题标题】:PHP Extracting the Contents of a DIV Block with domdocumentPHP 使用 domdocument 提取 DIV 块的内容
【发布时间】:2019-04-27 11:53:07
【问题描述】:

提取 DIV 块的内容。 Div 块中有更多块。我想检索一些信息。有几个 Div 块。最好能进入foreach循环。

$dom_document = new DOMDocument();
libxml_use_internal_errors(true);
$dom_document->loadHTML($html);
libxml_clear_errors();
$dom_document->preserveWhiteSpace = false;

//use DOMXpath to navigate the html with the DOM
$xpath = new DOMXpath($dom_document);

$items = $xpath->query('//div[contains(@class,"card")]');
foreach ($items as $item) {

   $title = $xpath->xpath('.//div[@class="card-header"]/div/a[@class="text-number"]');

}

echo $title;

Der HTML-Code dazu:


        <div class="row">
          <div class="col-xl-4 col-lg-6 col-md-6 col-smr-2 my-12 col-xs-12 mt-2 mb-3">
                    <div class="card">
                      <div class="card-header">
                        <div class="d-flex mb-2"><a class="text-number" href="/read/3325" id="3325">NUMBER</a>
                          </div>
                        </div>
                        <div class="d-flex">
                          <h5><a class="mr-auto" href="LINK" target="_blank">TITLE</a>
                          </h5>
                        </div>
                        <div class="d-flex"> <strong class="mr-2">AUTOR</strong><span class="mr-2">RANDOM-NUMBER</span>
                          <time class="text-muted mr-2" datetime="2019-04-26T01:20:28.000Z">TIME</time>
                        </div>
                      </div>
                      <div class="card-body">
                        <div class="card-text">CONTENT</div>
                      </div>
                    </div>
          </div>
          <div class="col-xl-4 col-lg-6 col-md-6 col-smr-2 my-12 col-xs-12 mt-2 mb-3">
                    <div class="card">
                      <div class="card-header">
                        <div class="d-flex mb-2"><a class="text-number" href="/read/3325" id="3325">NUMBER</a>
                          </div>
                        </div>
                        <div class="d-flex">
                          <h5><a class="mr-auto" href="LINK" target="_blank">TITLE</a>
                          </h5>
                        </div>
                        <div class="d-flex"> <strong class="mr-2">AUTOR</strong><span class="mr-2">RANDOM-NUMBER</span>
                          <time class="text-muted mr-2" datetime="2019-04-26T01:20:28.000Z">TIME</time>
                        </div>
                      </div>
                      <div class="card-body">
                        <div class="card-text">CONTENT</div>
                      </div>
                    </div>
          </div>
          <div class="col-xl-4 col-lg-6 col-md-6 col-smr-2 my-12 col-xs-12 mt-2 mb-3">
                    <div class="card">
                      <div class="card-header">
                        <div class="d-flex mb-2"><a class="text-number" href="/read/3325" id="3325">NUMBER</a>
                          </div>
                        </div>
                        <div class="d-flex">
                          <h5><a class="mr-auto" href="LINK" target="_blank">TITLE</a>
                          </h5>
                        </div>
                        <div class="d-flex"> <strong class="mr-2">AUTOR</strong><span class="mr-2">RANDOM-NUMBER</span>
                          <time class="text-muted mr-2" datetime="2019-04-26T01:20:28.000Z">TIME</time>
                        </div>
                      </div>
                      <div class="card-body">
                        <div class="card-text">CONTENT</div>
                      </div>
                    </div>
          </div>
          </div>

我需要以下信息 - 数字 - 关联 - 标题 - AUTOR - 随机数 - 时间 - 内容

非常感谢您的帮助。如果有人知道,那就太好了。

【问题讨论】:

    标签: php html xpath domdocument


    【解决方案1】:

    这是一个必须从您获得的每个 $item 内部开始的 XML 中挑选单个元素的情况。

    使用 DOMDocument,更容易坚持使用 XPath 表达式,但使用 descendant:: 来确保搜索的节点在指定为 evaluate() 的第三个参数的起始元素内。作为每个点,您都需要一个字符串值 - 我使用了 evaluate('string(...)),因为这将返回一个字符串,而 query() 将返回一个 DOMNodeList,然后您需要对其进行处理。

    鉴于上述情况,我希望 XPath 有意义,唯一奇怪的是内容。因为这是下一个节点(而不是子节点),所以我使用following-sibling:: 来访问它。

    $items = $xpath->query('//div[@class="card"]');
    foreach ($items as $item) {
        $title = $xpath->evaluate('string(descendant::*//a[@class="text-number"])', $item);
        $link = $xpath->evaluate('string(descendant::div[@class="d-flex"][1]/h5/a/@href)', $item);
        $title = $xpath->evaluate('string(descendant::div[@class="d-flex"][1]/h5/a)', $item);
        $autor = $xpath->evaluate('string(descendant::div[@class="d-flex"][2]/strong)', $item);
        $randomNumber = $xpath->evaluate('string(descendant::div[@class="d-flex"][2]/span)', $item);
        $time = $xpath->evaluate('string(descendant::div[@class="d-flex"][2]/time)', $item);
        $content = $xpath->evaluate('string(following-sibling::*//div[@class="card-text"])', $item);
    
        echo $title."/".$link."/".$title."/".$autor."/".$randomNumber.
            "/".$time."/".$content.PHP_EOL;
    }
    

    有关descendant::following-sibling:: 之类的更多信息,它们是在here 中讨论的XPath 轴。

    【讨论】:

    • 我想借此机会感谢您提供的详细信息和示例。除了 CONTENT,一切都是输出。
    • 希望你能追踪到它,这将是检查 HTML 和各种标记的情况(检查 CONTENT 的定义位置以及类属性是否恰好是 card-text
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多