2014-04-05 39 views
3

我试图抓取一个URL(http://sportzcosmos.com/2014/03/29/european-football-leagues-weekend-predictions/)。因为我能够将数据单独获取到像段落,标题的数组中。如何从页面抓取时获取数据

但我希望他们,因为他们在网站,我使用的是simple_php_dom。

我的代码如下:

foreach($article->find('article.post div.entry-content p') as $p){ 
     $articlecontent[] = $article->plaintext;   
    } 

同样地,我可以得到头也:

foreach($article->find('article.post div.entry-content h2') as $h){ 
     $articlecontent[] = $article->plaintext;   
    } 

但我想,让他们以作为他们在网站;有什么方法可以让这些数据有序?

回答

1

的一种方式做,这是在同一回路的同时找到两个......

这是一个工作代码:

$url = "http://sportzcosmos.com/2014/03/29/european-football-leagues-weekend-predictions/"; 

//Create a DOM object 
$html = new simple_html_dom(); 
// Load HTML from a url 
$html->load_file($url); 

$articlecontent = array(); 

foreach($html->find('article.post div.entry-content p, article.post div.entry-content h2') as $article){ 
    $articlecontent[] = $article->plaintext; 
} 

print_r($articlecontent); 

输出

enter image description here

+0

非常感谢!它的工作和解决了我的大部分问题... – anand