【问题标题】:Website Scraping from DoMDocument using php使用 php 从 DoMDocument 抓取网站
【发布时间】:2021-02-01 18:11:09
【问题描述】:

我有一个可以提取类别并显示它们的 php 代码。然而, 我仍然无法提取随之而来的数字(没有括号)。 需要将类别和编号分开(不要一起提取)。 也许使用正则表达式等做另一个 for 循环......

这是代码:

<?php
    $grep = new DoMDocument();
    @$grep->loadHTMLFile("http://www.lelong.com.my/Auc/List/BrowseAll.asp");

    $finder = new DomXPath($grep);
    $class = "CatLevel1";
    $nodes = $finder->query("//*[contains(@class, '$class')]");

    foreach ($nodes as $node) {
        $span = $node->childNodes;
        echo $span->item(0)->nodeValue."<br>";
    }
?>

有什么办法可以做到吗?谢谢!

这是我想要的输出:

Arts, Antiques & Collectibles : 9768<br>
B2B & Industrial Products : 2342<br>
Baby : 3453<br>
etc...

【问题讨论】:

    标签: php web


    【解决方案1】:

    只需添加另一个兄弟姐妹。示例:

    foreach ($nodes as $node) {
        $span = $node->childNodes;
        echo $span->item(0)->nodeValue . ': ' . str_replace(array('(', ')'), '', $span->item(1)->nodeValue);
        echo '<br/>';
    }
    

    编辑:只需使用 str_replace 即可删除括号。

    旁注:始终将 UTF-8 编码放在您的 PHP 文件中。

    header('Content-Type: text/html; charset=utf-8');
    

    【讨论】:

    • 感谢您的回复...这是输出 Arts, Antiques & Collectibles: Â (9768) 如何消除 Â 和括号?
    • @Cael 查看修订版
    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多