简体   繁体   English

如何抓取内联 css?

[英]How can I scrape inline css?

I am using simple_html_dom script for getting information from a site.我正在使用 simple_html_dom 脚本从站点获取信息。

I am trying to scrape an element that carrying the display: none property.我正在尝试抓取一个带有 display: none 属性的元素。

Here's the element:这是元素:

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

How can I identify that this tag carries inline CSS display: none;?如何识别此标签带有内联 CSS display: none;?

Here's my code:这是我的代码:

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

$links->outertext is giving me only this: $links->outertext只给了我这个:

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

You can see that, it's not including the style property while scraping.你可以看到,它在抓取时不包括 style 属性。

So how can I get the inline CSS property?那么如何获得内联 CSS 属性呢? If I have to use a different library, please suggest.如果我必须使用不同的库,请提出建议。

You can get style using ->style .你可以style使用->style Try this:尝试这个:

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

Doable for me as expected:正如预期的那样对我来说是可行的:

<?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'";

The output obviously is:输出显然是:

display set to 'none'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM