user1788542
user1788542

Reputation:

Is it possible to scrape data from html element with specific class

I worked out various regex to scrape the data.

Here I can scrape image from the page source:

Here I scraped data from table td

    <?php

    $s = file_get_contents('http://www.altassets.net/altassets-events'); 
    $matches = array(); 
    preg_match_all("/<tr>(.*)<\/tr>/sU", $s, $matches); 
    $trs = $matches[1]; $td_matches = array(); 
    foreach ($trs as $tr) { $tdmatch = array(); 
    preg_match_all("/<td>(.*)<\/td>/sU", $tr, $tdmatch); 
    $td_matches[] = $tdmatch[1]; } var_dump($td_matches); 
    //print_r($td_matches); 
?>

similarly image and titles too.

But how to scrape data from <p> tag with specific class name?

<p class="review_comment ieSucks" itemprop="description" lang="en"> Some text </p>

Consider this page,

http://www.yelp.com/biz/fontanas-italian-restaurant-cupertino

this is just example, just want to know procedure. class name and tag name can be changed

I want to scrape review and it's Rate value from the page

Upvotes: 3

Views: 3815

Answers (3)

Mihir Bhatt
Mihir Bhatt

Reputation: 3155

Here is the complete example of data scrap + get element by classname

    function get_web_page( $url )
    {
        $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
        $options = array(
            CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
            CURLOPT_POST           =>false,        //set to GET
            CURLOPT_USERAGENT      => $user_agent, //set user agent
            CURLOPT_COOKIEFILE     =>"cookie.txt", //set cookie file
            CURLOPT_COOKIEJAR      =>"cookie.txt", //set cookie jar
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => false,    // don't return headers
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        );
        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );

        $dom = new DOMDocument();
        $dom->loadHTML($content);
        $finder = new DomXPath($dom);
        $classname="CLASS_NAME";
        $nodes = $finder->query("//*[contains(@class, '$classname')]");

        foreach ($nodes as $key => $ele) {
            print_r($ele->nodeValue);
        }
    }

    get_web_page('DATA_SCRAP_URL_GOES_HERE');

Upvotes: 1

Don't use Regular expressions. Implement PHP native DOMXPath or DOMDocument Class..

foreach($dom->getElementsByTagName('p') as $ptag)
{
    if($ptag->getAttribute('class')=="review_comment ieSucks")
    {
        echo $ptag->nodeValue; //"prints" Some text
    }
}

Loop through all the paragraph tags and see if there is match found on attribute, if found, you could just print the node's value.

Working Demo

Using file_get_contents()

<?php
libxml_use_internal_errors(true);
$html=file_get_contents('http://www.yelp.com/biz/fontanas-italian-restaurant-cupertino');
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach($dom->getElementsByTagName('p') as $ptag)
{
    if($ptag->getAttribute('class')=="review_comment ieSucks")
    {
        echo "<h6>".$ptag->nodeValue."</h6>";
    }
}

Upvotes: 1

skywalker
skywalker

Reputation: 826

You can use Simple HTML Dom parser for this.

Usage is pretty simple:

// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');

and then you can do something like this:

// Find all element which id=foo
$ret = $html->find('#foo');

// Find all element which class=foo
$ret = $html->find('.foo');

Upvotes: 0

Related Questions