2

我可以使用

$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">
4

3 回答 3

0

尝试使用这个。非常适合我并且非常易于使用。http://code.google.com/p/phpquery/

于 2011-05-23T05:36:15.047 回答
0

简单的 HTML DOM 可以很容易地用于查找具有特定类的 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 />";
}

使用您的示例对我有用,我的输出是:

标签=跨度,类=位置:找到 1

0:加拿大乔治亚山脉

希望对您有所帮助......并且请简单的 HTML DOM 非常适合它的功能,并且一旦您掌握了它就易于使用。继续尝试,您将获得许多您一遍又一遍地使用的示例。我已经刮掉了一些非常疯狂的页面,它们变得越来越容易。

于 2011-07-02T00:56:53.230 回答
0

PHP Simple DOM parser 上的文档在解读 Open Graph 元标记方面参差不齐。这似乎对我有用:

<?php
// grab the contents of the page
$summary = file_get_html($url);

// Get image possibilities (for example)

$img = array();

// First, if the webpage has an og:image meta tag, it's easy:
if ($summary->find('meta[property=og:image]')) {
  foreach ($summary->find('meta[property=og:image]') as $e) {
    $img[] = $e->attr['content'];
  }
}
?>
于 2012-01-27T19:56:34.817 回答