【问题标题】:How can I scrape inline css?如何抓取内联 CSS?
【发布时间】:2017-07-15 07:13:24
【问题描述】:

我正在使用 simple_html_dom 脚本从网站获取信息。

我正在尝试抓取一个带有 display: none 属性的元素。

这是元素:

<label data-product-attribute-value="1307" class="form-label" for="attribute_1307" style="display: none;">This is the title</label>

如何识别此标记带有内联 CSS display: none;?

这是我的代码:

$html  = get_html_data($url);
foreach ($html->find('.form-label') as $links) {
   echo $links->outertext;
}

$links-&gt;outertext 只给我这个:

<label data-product-attribute-value="1307" class="form-label" for="attribute_1307">This is the title</label>

你可以看到,它在抓取时不包括样式属性。

那么我怎样才能获得内联 CSS 属性呢?如果我必须使用其他库,请提出建议。

【问题讨论】:

    标签: php scrape


    【解决方案1】:

    您可以使用-&gt;style 获取style。试试这个:

    foreach ($html->find('.form-label') as $links) {
       echo ($links->style); //op : display: none;
    }
    

    【讨论】:

      【解决方案2】:

      按预期对我来说可行:

      <?php
      $html = <<< HTML
      <label data-product-attribute-value="1307" class="form-label" for="attribute_1307" style="display: none;">
      This is the title
      </label>
      HTML;
      
      $dom = new DOMDocument;
      $dom->loadHTML($html);
      echo (   $label->hasAttribute('style')
            && preg_match('/display:\s*none/', $label->getAttribute('style')) ) ?
          "display set to 'none'" : "display NOT set to 'none'";
      

      输出明显是:

      display set to 'none'
      

      【讨论】:

      • 感谢您的建议。我发现 file_get_html 也没有获取该特定标签的样式属性。我得到了少数元素的样式属性,但不是全部。我还使用 curl 通过 URL 废弃所有 HTML,但同样的问题来了。请提出建议。
      猜你喜欢
      • 1970-01-01
      • 2015-06-11
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 2021-09-10
      • 2019-05-15
      相关资源
      最近更新 更多