0

I am using php DomDocument to scrape multiple tables but i don't understand how to i output multiple tables data as array, like below example.

Example:

[0] =>
    [Network] =>
                [Technology]=>[GSM / HSPA / LTE]
                [...]=>[...],
                [...]=>[...]
[1] =>
    [...] =>
                [...]=>[...],
                [...]=>[...],
                [...]=>[...]

My code is below:

$doc = new DomDocument();
$doc->preserveWhiteSpace = false;
@$doc->loadHTML($responseBody);
$xpath = new DOMXPath($doc);

$th = $xpath->query('//table//th');
$tdFirst = $xpath->query('//table//td[contains(@class, "ttl")]');
$tdSecond = $xpath->query('//table//td[contains(@class, "nfo")]');

i get output from below code but i don't want this type output. I need to output like my example. I want to display outputted data on my own table design, so i need it.

foreach($th as $rows) {
  echo $rows->nodeValue.'<br>';
}

foreach($tdFirst as $rows) {
  echo $rows->nodeValue.'<br>';
}

foreach($tdSecond as $rows) {
  echo $rows->nodeValue.'<br>';
}

Sorry for not good english. Thanks

I want to scrape data from multiple table like below table (i just copy and pasted first table to many times for understand you, my targeted scraping site tables are look same ):

<table>
<tbody>
<tr>
<th scope="col" colspan="2">Network</th>
</tr>
<tr>
<td class="ttl"><a>Technology</a></td>
<td class="nfo" >GSM / HSPA / LTE</td>
</tr>
</tbody>
</table>

<table>
    <tbody>
    <tr>
    <th scope="col" colspan="2">Network</th>
    </tr>
    <tr>
    <td class="ttl"><a>Technology</a></td>
    <td class="nfo" >GSM / HSPA / LTE</td>
    </tr>
    </tbody>
    </table>

<table>
    <tbody>
    <tr>
    <th scope="col" colspan="2">Network</th>
    </tr>
    <tr>
    <td class="ttl"><a>Technology</a></td>
    <td class="nfo" >GSM / HSPA / LTE</td>
    </tr>
    </tbody>
    </table>

<table>
    <tbody>
    <tr>
    <th scope="col" colspan="2">Network</th>
    </tr>
    <tr>
    <td class="ttl"><a>Technology</a></td>
    <td class="nfo" >GSM / HSPA / LTE</td>
    </tr>
    </tbody>
    </table>
  • Please provide a clear example of what you need given your table demo (input -> output). Right now your output example does not match your html, so we have to guess what you want. – Jeto Apr 28 '19 at 19:56
  • Sorry, i updated my post. I want to scrape data from these type HTML codes – Rakib Hasan Apr 28 '19 at 20:06
  • I want to scrape data from my posted html table code then i want to output data like my first example. 2nd html example for scraping data, so showed table structure on here. also my english is not good so i fetching problem to understand you. – Rakib Hasan Apr 28 '19 at 20:11
  • After scraped, output will be showed on my own design table. Such as: `Network` `Technology: GSM / HSPA / LTE ` – Rakib Hasan Apr 28 '19 at 20:19
  • Good news" or "Bad news" its example. After some times submitted post i updated again html code. – Rakib Hasan Apr 28 '19 at 20:21
  • Edit your post with **exactly** what you want given that example, and the scraping "rules" (such as: "for each `th`, take the cells of the following `tr` and group its values by them" or something along those lines). We can't guess what the HTML could be (multiple ``s, etc.) – Jeto Apr 28 '19 at 20:23

0 Answers0