0

I want to grab first 10 results of that any url i passed it to function as parameter ..and i want to make data scraper for some sites

Getting Syntax error when I print the result on screen syntax error on this line I don't why it giving me syntax error kindly help me....

print_r( $dse->crawl()->parse() );

<?php               
      class CURL_CRAWLER{
      public $url;
      public $request_type;
      public $data;
      public $post_params;

  function __construct($url = '' , $request_type = 'GET')
  {
     $this->url = $url;
     $this->request_type = $request_type;
     $this->data = '';
     $this->post_params = array();
   }
/**crawl a document **/
 function crawl()
   {
    $curl = curl_init( $this->url );
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 60);
    curl_setopt($curl, CURLOPT_USERAGENT, 'cURL PHP');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $this->data = curl_exec($curl);
    curl_close($curl);
    return $this; //make it a chainable method
   }
/** Parse result data **/
   function parse(){
   $result = array();
   $count = 0;
   $dom = new DOMDocument;
   $dom->preserveWhiteSpace = false;
   $dom->loadHTML($this->data);
   $xpath = new DOMXPath($dom);
   $news = $xpath->query('//td[@bgcolor="#DDDDDD"]/table/tr[position()=2]/td[position()=2]');
   foreach( $news as $n){
       $result[] =   $n->nodeValue;
       $count++;
       if ($count >9)
           break; //we just need  10 results. Index starts from 0
   }
   return $result;
      }
    }
 error_reporting(0);
        $dse = new CURL_CRAWLER('http://www.dsebd.org/display_news.php');
    echo "<pre>";
    print_r( $dse-&gt;crawl()-&gt;parse() );
    echo "<pre>";

   ?>
5
  • move <?php before print_r Commented Jan 7, 2016 at 14:28
  • i don't understand wat u want to say actually kindly elobrate Commented Jan 7, 2016 at 14:32
  • if the above code is exactly how your code looks like, the syntax error is that php opening tag is not at the beginning, it should be the first thing Commented Jan 7, 2016 at 14:34
  • now check the code buddy Commented Jan 7, 2016 at 14:41
  • 1
    you need to change -&gt; to -> in print_r argument Commented Jan 7, 2016 at 14:44

1 Answer 1

2

Your syntax error is that you should use explicit "greater than" sign instead of HTML entities &gt; - server doesn't need those, it is not a browser that can render it correctly. Just change:

print_r( $dse-&gt;crawl()-&gt;parse() );
              ^^^^        ^^^^

to:

print_r( $dse->crawl()->parse() );
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.