php - 如何从具有特定背景颜色的li链接获取内容?
问题描述:
外部网站,我试图让这样从长相内容:php - 如何从具有特定背景颜色的li链接获取内容?
<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>
这是我到目前为止,虽然我只想要得到李行的内容与风格:“背景色:RGB (252,187,69);”。我可以将它添加到$查询吗?
<?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);
?>
非常感谢。
答
如何从具有特定背景颜色的li链接获取内容?
<?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'));
?>
这是我想到了。尽管查询似乎还没有找到它。还有其他样式的链接,如“文字大胆”等;这会影响查询吗?它有多严格? – Kaiwen
它完全匹配它,你从来没有谈论过额外的风格属性。你可能会更好,只需找到所有链接,然后在循环中使用'getAttribute('style')',然后使用strstr()来检查特定的样式。 –
啊好吧...所以它完全匹配。谢谢,现在工作。 – Kaiwen