php - How to get the specific <td> while scraping a web page table using a DOM? -
i've table
, of number of columns can change depending on configuration of scrapped page (i have no control of it). want information specific column, designated columns heading.
sample table:
<table> <tr> <td>name</td> <td>age</td> <td>marks</td> </tr> <tr> <td>a</td> <td>20</td> <td>90</td> </tr> <tr> <td>b</td> <td>21</td> <td>80</td> </tr> <tr> <td>c</td> <td>22</td> <td>70</td> </tr> </table>
my working php code display columns:
foreach($html->find("table#table2 tr td") $td) { $code = $td; echo $code; }
needed code format:
foreach($html->find('table#table2 td') $td) { /* td1 data */ /* code1 store td data 1 */ /* next td data */ /* code2 store td data 2 */ /* next td data */ /* code3 store td data 3 */ }
i want extract output , store db table having table name result in appropriate columns.
i can write storing code myself. need code retrieve consecutive td data inside row without loop.since code store td data varies.
posts referred - scraping webpage.
// create dom url or file $html = file_get_html("http://www.example.org/"); // find tr array $tr_array = $html->find("table#table2 tr"); $td_array = []; // find td array foreach($tr_array $tr) { array_push($td_array,$tr->find("td")); } echo "<table id=\"table1\">"; foreach($tr_array $tr) { echo "<tr>"; foreach($td_array $td) { echo $td; } echo "</tr>"; } echo "</table>";
for advanced topics, read simplehtmldom.
in above code, i've stored array objects inside arrays:
<?php $a = []; $a1 = [1,2,3]; $a2 = [4,5,6]; array_push($a,$a1,$a2); foreach($a $a_e) { foreach($a_e $e) { echo $e; } echo "<br>"; } ?>
outputs:
123
456
Comments
Post a Comment