【问题标题】:Need help for fixing html when scrape using php simple html dom [duplicate]使用 php simple html dom 抓取时需要帮助修复 html [重复]
【发布时间】:2021-09-02 12:28:11
【问题描述】:
    require_once 'phpDomClass.php';
    $html = '<div> 
            <div class="man">Name: madac</div>
            <div class="man">Age: 18
            <div class="man">Class: 12</div>
</div>' 
    $name=$html->find('div[class="man"]', 0)->innertext;
    $age=$html->find('div[class="man"]', 1)->innertext; 
    $cls=$html->find('div[class="man"]', 2)->innertext;

想从每个 div class="man" 获取文本,但它不起作用,因为 html 代码的第 2 行缺少结束 div 标记。请帮我解决这个问题。

提前致谢。

【问题讨论】:

    标签: php html


    【解决方案1】:

    在这种情况下,您必须先进行一些难看的字符串操作,然后再尝试使用 DOM 解析器对其进行解析。如果这是一次性的,你可以做这样的事情

    <?php
    require_once 'phpDomClass.php';
    $html = '<div> 
      <div class="man">Name: madac</div>
      <div class="man">Age: 18
      <div class="man">Class: 12</div>
    </div>';
    $html = str_replace('<div class="man">Class:','</div><div class="man">Class:',$html);
    
    $dom = new DomDocument();
    $dom->loadHTML($html);
    $name=$html->find('div.man', 0)->innertext;
    $age=$html->find('div.man', 1)->innertext; 
    $cls=$html->find('div.man', 2)->innertext;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      相关资源
      最近更新 更多