0

Hello I write following code to extract name and price from table with XPATH and curl.

   <?php
    include_once ("xpath.php");
    header('Content-type: text/html; charset=UTF-8');
    $ch = curl_init ("http://emalls.ir/%D9%84%DB%8C%D8%B3%D8%AA-%D9%82%DB%8C%D9%85%D8%AA~Category~39~Search~Nokia");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //$page = curl_exec($ch);
    $page = utf8_decode(curl_exec($ch));

    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML($page);
    libxml_clear_errors();
    $xpath = new DOMXpath($dom);
    $data = array();


    // get all table rows and rows which are not headers
    $produstname = $xpath->query('//table/tbody/tr/td/a/text()');
    $produstprice = $xpath->query('//table/tbody/tr/td[8]/text()');
    $data = array();
    for ($x=0; $x<=1; $x++){
        $data[$x]['title'] = $produstname->item($x)->nodeValue;
        $data[$x]['price'] = $produstprice->item($x)->nodeValue;
    }
    ?>

These following two XPATH working on chrome to get name and price .

 name: $x("//table/tbody/tr/td/a/text()")
 price: $x("//table/tbody/tr/td[5]/text()")

but when use in following code give this error

 : Trying to get property of non-object in 
4
  • 1
    "in..." in what?! The suspense is killing me Commented Sep 5, 2014 at 5:59
  • Could it be, just maybe, that there aren't 20 elements matching your XPath queries? Commented Sep 5, 2014 at 6:01
  • when I writed 1 elment give this error. Commented Sep 5, 2014 at 6:02
  • 1
    Biggest problem I can see is that there are no <tbody> elements Commented Sep 5, 2014 at 6:06

1 Answer 1

1

I've seen the site, I humbly suggest target the id="" attribute instead. You can also use foreach too. Example:

$ch = curl_init ("http://emalls.ir/%D9%84%DB%8C%D8%B3%D8%AA-%D9%82%DB%8C%D9%85%D8%AA~Category~39~Search~Nokia");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$page = curl_exec($ch);
$page = utf8_decode(curl_exec($ch));

$dom = new DOMDocument('1.0', 'utf-8');
libxml_use_internal_errors(true);
$dom->loadHTML($page);
libxml_clear_errors();
$xpath = new DOMXpath($dom);

$data = array();
$table_rows = $xpath->query('//table[@id="grdprice"]/tr'); // target the row (the browser rendered <tbody>, but actually it really doesnt have one)

if($table_rows->length <= 0) { // exit if not found
    echo 'no table rows found';
    exit;
}

foreach($table_rows as $tr) { // foreach row
    $row = $tr->childNodes;
    if($row->item(0)->tagName != 'th') { // avoid headers
        $data[] = array(
            'name' => trim($row->item(0)->nodeValue),
            'price' => trim($row->item(7)->nodeValue),
        );
    }
}

echo '<pre>';
print_r($data);

Sample Output

Sign up to request clarification or add additional context in comments.

10 Comments

Thank's.You're a professional PHP . why $data[] .. is Empty
@amirrasabeh thanks for the complement, but no, i haven't even scratched the surface of PHP, still a long ways to go
@amirrasabeh that is just simply stating that if you do not explicitly put an index on the assignment, it will append it on the end of the array
Do you have email. I want Always to be in touch with you
@amirrasabeh no need to email me, if i can help you inside the scope of SO and seen your question, i'll try to answer if i know
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.