php - 如何从具有特定背景颜色的li链接获取内容?
The external website I'm trying to get content from looks like this:
<div class="the_div_class">
<ul>
<li><a href="" style="background-color: rgb(252,187,69);">inferno</a></li>
<li><a href="" style="background-color: rgb(251,212,142);">heat</a></li>
</ul>
</div>
This is what I have so far though I only want to get li row content with the style: "background-color: rgb(252,187,69);". Can I add this somehow to the $query?
<?php
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$doc->loadHTMLFile('http://www.the_website.com');
$xpath = new DOMXPath($doc);
$query = "//div[@class='the_div_class']";
$entries = $xpath->query($query);
var_dump($entries->item(0)->textContent);
?>
Thanks a lot.
我试图获取内容的外部网站如下所示: p>
这是我到目前为止的情况,虽然我只想获得具有样式的li行内容: background-color:rgb(252,187,69);“。 我能以某种方式将其添加到$查询中吗? p>
非常感谢。 p>
div>&lt; div class =“the_div_class”&gt;
&lt; ul&gt;
&lt; li&gt;&lt; a href =“”style =“background-color:rgb(252,187,69);”&gt ; inferno&lt; / a&gt;&lt; / li&gt;
&lt; li&gt;&lt; a href =“”style =“background-color:rgb(251,212,142);”&gt;加热&lt; / a&gt;&lt; / li&gt; \ n&lt; / ul&gt;
&lt; / div&gt;
code> pre>
&lt;?php
$ doc = new DOMDocument;
$ doc-&gt; preserveWhiteSpace = false;
$ doc-&gt; strictErrorChecking = false;
$ doc-&gt; recover = true;
$ doc-&gt; loadHTMLFile('http://www.the_website.com');
$ xpath = new DOMXPath($ doc);
$ query = “// div [@ class ='the_div_class']”;
$ entries = $ xpath-&gt; query($ query);
var_dump($ entries-&gt; item(0) - &gt; textContent);
?&gt;
code> pre>
How to get content from an li link with a specific background color?
<?php
...
$query = "//a[@style='background-color: rgb(252,187,69);']";
$entries = $xpath->query($query);
// get text
var_dump($entries->item(0)->textContent);
// get link
var_dump($entries->item(0)->getAttribute('href'));
?>