问题
im using simple html dom parser to get scrap some data from html string .
i need to return the TD's values from a table with specific css class each TD as array element
i tried this cod but it gives fetal error
<?php
include('classes/simple_html_dom.php');
$html = str_get_html('<table class="pages_navigator">
<tr>
<th style="width:50px;">ID </th>
<th>Business </th>
<th style="width:70px;">Category</th>
<th style="width:50px;">Phone </th>
<th style="width:70px;">State</th>
<th style="width:70px;">City</th>
<tr class="users_tr">
<td>3571</td>
<td>Premium GD</td>
<td>2063199876</td>
<td>Washington</td>
<td>Seattle</td>
<td>3703</td>
</tr>
</table>');
$tds = $html->find('table.pages_navigator')->find('td') ;
print_r($tds);
?>
then i tried
<?php
include('classes/simple_html_dom.php');
$html = str_get_html('<table class="pages_navigator">
<tr>
<th style="width:50px;">ID </th>
<th>Business </th>
<th style="width:70px;">Category</th>
<th style="width:50px;">Phone </th>
<th style="width:70px;">State</th>
<th style="width:70px;">City</th>
<tr class="users_tr">
<td>3571</td>
<td>Premium GD</td>
<td>2063199876</td>
<td>Washington</td>
<td>Seattle</td>
<td>3703</td>
</tr>
</table>');
$result = array();
foreach($html->find('tr.users_tr') as $e){
$result[] = $e->plaintext . '<br>';
}
print_r($result);
?>
the last one worked good but it brings all TD;s as a single string no each td as an array element ? var_dump result
Array (
[0] => 3571 Premium GD 2063199876 Washington Seattle 3703
)
回答1:
Change your query from
foreach($html->find('tr.users_tr') as $e){
to
foreach($html->find('tr.users_tr td') as $e){
This should allow you to iterate through all of the td's instead of getting the plain text of the whole row.
来源:https://stackoverflow.com/questions/17691175/simple-html-dom-parser-to-return-all-tds-values