我可以访问一些“类”项目
$ret = $html->find('articleINfo'); and then print the first key of the returned array.
但是,我还需要其他标签,例如 span=id"firstArticle_0",但我似乎找不到。
$ret = $html->find('#span=id[ etc ]');
在某些情况下返回了一些东西,但它不是一个数组,或者是一个带有空键的数组。
不幸的是,我无法使用 var_dump 查看该对象,因为 var_dump 会产生 1000 页不可读的垃圾。代码看起来像这样。
<div id="articlething">
<p class="byline">By Lord Byron and <a href="www.marriedtothesea.com">Alister Crowley</a></p>
<p>
<span class="location">GEORGIA MOUNTAINS, Canada</span> |
<span class="timestamp">Fri Apr 29, 2011 11:27am EDT</span>
</p>
</div>
<span id="midPart_0"></span><span class="mainParagraph"><p><span class="midLocation">TUSCALOOSA, Alabama</span> - Who invented cheese? Everyone wants to know. They held a big meeting. Tom Cruise is a scientologist. </p>
</span><span id="midPart_1"></span><p>The president and his family visited Chuck-e-cheese in the morning </p><span id="midPart_2"></span><p>In Russia, 900 people were lost in the balls.</p><span id="midPart_3">
最佳答案
可以使用简单的 HTML DOM 轻松查找具有特定类的 span。
如果想要所有 span 的 class=location 那么:
// create HTML DOM
$html = file_get_html($iUrl);
// get text elements
$aObj = $html->find('span[class=location]');
然后做类似的事情:
foreach($aObj as $key=>$oValue)
{
echo $key.": ".$oValue->plaintext."<br />";
}
使用您的示例对我有用,我的输出是:
label=span, class=location: 找到 1
0:乔治亚山,加拿大
希望对您有所帮助...请 简单的 HTML DOM 非常适合它的功能,并且一旦您掌握了它就很容易使用。继续尝试,您将获得大量反复使用的示例。我抓取了一些非常疯狂的页面,它们变得越来越容易。
关于php - 您如何访问简单的 DOM 选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5834262/