Question

I am trying to scrape one of the URL (http://sportzcosmos.com/2014/03/29/european-football-leagues-weekend-predictions/) . Since I am able to get the data individually into an array like paragraph, header.

But I want them in order as they are in the website , I am using simple_php_dom .

My code is as follows:

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

Similarly I can get the header also:

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

But I want to get them in order as they are in the website ; Is there any way out to get these data in order?

Était-ce utile?

La solution

A way to do this is to find both at the same time in the same loop...

This is a working code:

$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);

Output

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top