Saturday, 15 March 2014

Scraping data from the table using Xpath and PHP? -



Scraping data from the table using Xpath and PHP? -

i want extract info table below , markup of table given below.i'm using xpath extract info table other suggestions welcome.

<div style="clear:both;" id="showprice"> <br> <table cellspacing="1"> <tbody> <tr> <td width="50px" style="text-align: left" class="tdhead">sn</td> <td width="650px" style="text-align: left" class="tdhead">companies</td> <td width="20px" class="tdhead">trans</td> <td width="50px" class="tdhead"> max price</td> <td width="50px" class="tdhead">min price</td> <td width="50px" class="tdhead">closing price</td> <td width="50px" class="tdhead">total shares</td> <td width="50px" class="tdhead">amount rs.</td> <td width="50px" class="tdhead">prev. closing</td> <td width="20px" class="tdhead">diff.</td> <td width="50px" class="tdhead">diff. %</td> <td colspan="3" class="closing-price"> <table> <tbody> <tr> <td colspan="3">365&nbsp;days</td> </tr> <tr> <td width="50px" class="closing-price-lighter">max price</td> <td width="50px" class="closing-price-lighter">min price</td> <td width="50px" class="closing-price-lighter">avg</td> </tr> </tbody> </table> </td> </tr> <tr style="background-color: #a61a00"> <td style="text-align: center;color:white;">1</td> <td style="text-align: left;padding:3px;"> <a href="viewcompany.php?symbol=acedbl&amp;id=177" style="text-decoration:none;color:white;">ace development bank limited</a> </td> <td class="numeric-data">3</td> <td class="numeric-data">269.00</td> <td class="numeric-data">264.00</td><td class="numeric-data" style="background-color:#99ccff;color:black;">264.00</td> <td class="numeric-data">495</td> <td class="numeric-data">131,405</td> <td class="numeric-data">265.00</td> <td class="numeric-data">-1.00</td> <td class="numeric-data" style="background-color:#99ccff;color:black;">-0.38</td> <td class="numeric-data" style="background-color:#99ffff;color:black;">281</td> <td class="numeric-data" style="background-color:#99ffff;color:black;">102</td> <td class="numeric-data" style="background-color:#99ffff;color:black;">150.15</td> </tr> </tbody> </table> </div>

i want info after closing-price class. info require text , numeric value next td of tr:

<td style="text-align: left;padding:3px;"> <a href="viewcompany.php?symbol=acedbl&amp;id=177" style="text-decoration:none;color:white;">ace development bank limited</a> </td> <td class="numeric-data">3</td> <td class="numeric-data">269.00</td> <td class="numeric-data">264.00</td><td class="numeric-data" style="background-color:#99ccff;color:black;">264.00</td> <td class="numeric-data">495</td> <td class="numeric-data">131,405</td> <td class="numeric-data">265.00</td> <td class="numeric-data">-1.00</td> <td class="numeric-data" style="background-color:#99ccff;color:black;">-0.38</td> <td class="numeric-data" style="background-color:#99ffff;color:black;">281</td> <td class="numeric-data" style="background-color:#99ffff;color:black;">102</td> <td class="numeric-data" style="background-color:#99ffff;color:black;">150.15</td> </tr>

i tried next look not result:

//div[@id='showprice']/td[preceding-sibling::td[@class='closing-price']]/text()

you can somethin this:

point particular <tr> tag:

$html_string = file_get_contents('http://www.sharesansar.com/today.php'); $dom = new domdocument(); libxml_use_internal_errors(true); $dom->loadhtml($html_string); libxml_clear_errors(); $xpath = new domxpath($dom); $values = array(); $row = $xpath->query('//div[@id="showprice"]/table[1]/tr[2]/td'); foreach($row $value) { $values[] = trim($value->textcontent); } echo '<pre>'; print_r($values);

the results:

array ( [0] => 1 [1] => ace development bank limited [2] => 3 [3] => 269.00 [4] => 264.00 [5] => 264.00 [6] => 495 [7] => 131,405 [8] => 265.00 [9] => -1.00 [10] => -0.38 [11] => 281 [12] => 102 [13] => 150.15 )

php xpath web-scraping

No comments:

Post a Comment