【问题标题】:Simple HTML DOM Not Finding DIV简单的 HTML DOM 找不到 DIV
【发布时间】:2014-01-26 00:52:29
【问题描述】:

我有代码试图从机器人事件页面here is an example 中提取事件 SKU。我使用的代码在页面上找不到任何 SKU。 SKU 位于第 411 行,具有“product-sku”类的 div。我的代码没有找到页面上的 Div,只是下载了所有事件。这是我的代码:

<?php
require('simple_html_dom.php');
$html = new simple_html_dom();
if(!$events)
{
    echo mysqli_error($con);
}
while($event = mysqli_fetch_row($events))
{
    $htmldown = file_get_html($event[4]);
    $html->load($htmldown);
    echo "Downloaded";
    foreach ($html->find('div[class=product-sku]') as $row) {
       $sku = $row->plaintext;
       echo $sku;
    }
}
?>

谁能帮我修复我的代码?

【问题讨论】:

  • load后变量$htmldown的值是多少?

标签: php html dom


【解决方案1】:

这段代码使用了DOMDocument php 类。它适用于以下示例 HTML。请尝试此代码。

// new dom object
$dom = new DOMDocument();

// HTML string
$html_string = '<html>
                    <body>  
                          <div class="product-sku1" name="div_name">The this the div content product-sku</div>
                          <div class="product-sku2" name="div_name">The this the div content product-sku</div>
                          <div class="product-sku" name="div_name">The this the div content product-sku</div>
                    </body>
                </html>';

//load the html
$html = $dom->loadHTML($html_string);

//discard white space 
$dom->preserveWhiteSpace = TRUE;

//the table by its tag name
$divs = $dom->getElementsByTagName('div');

// loop over the all DIVs
foreach ($divs as $div) {
    if ($div->hasAttributes()) {
        foreach ($div->attributes as $attribute){
            if($attribute->name === 'class' && $attribute->value == 'product-sku'){
                // Peri DIV class name and content
                echo 'DIV Class Name: '.$attribute->value.PHP_EOL;
                echo 'DIV Content: '.$div->nodeValue.PHP_EOL;
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    我会使用正则表达式(正则表达式)来完成将 skus 拉出。

    正则表达式:

    preg_match('~&lt;div class="product-sku"&gt;&lt;b&gt;Event Code:&lt;/b&gt;(.*?)&lt;/div&gt;~',$html,$matches);

    php regex docs

    新代码:

    <?php
    if(!$events)
    {
        echo mysqli_error($con);
    }
    while($event = mysqli_fetch_row($events))
    {
        $htmldown = curl_init($event[4]);
        curl_setopt($htmldown, CURLOPT_RETURNTRANSFER, true);
        $html=curl_exec($htmldown);
        curl_close($htmldown)
        echo "Downloaded";
    
        preg_match('~<div class="product-sku"><b>Event Code:</b>(.*?)</div>~',$html,$matches);
    
    
        foreach ($matches as $row) {
           echo $row;
        }
    }
    ?>
    

    实际上在这种情况下(使用该网页)只有一个 sku...

    代替:

    foreach ($matches as $row) {
        echo $row;
    }
    

    您可以只使用:echo $matches[1];(数组索引 1 的原因是因为整个正则表达式模式加上 sku 将在 $matches[0] 中,但只有包含 sku 的子组在 $matches[1] 中。 )

    【讨论】:

      【解决方案3】:

      尝试使用

      require('simple_html_dom.php');
      $html = new simple_html_dom();
      if(!$events)
      {
          echo mysqli_error($con);
      }
      while($event = mysqli_fetch_row($events))
      {
          $htmldown = str_get_html($event[4]);
          echo "Downloaded";
          foreach ($htmldown->find('div[class=product-sku]') as $row) {
              $sku = $row->plaintext;
              echo $sku;
          }
      }
      

      如果类“product-sku”仅适用于 div,那么您可以使用

       $htmldown->find('.product-sku')
      

      【讨论】:

        猜你喜欢
        • 2018-04-07
        • 1970-01-01
        • 2018-02-03
        • 2015-01-07
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2019-08-06
        • 2016-10-03
        相关资源
        最近更新 更多