how to scrape this with Simple HTML DOM [closed]

2020-04-01 07:14发布

I'm trying to use simple html dom to extract elements from a file that looks like this.

  • The file has several tables that look the same class=sometable.
  • Each table has a few <tr class=sometr>.
  • Then inside each tr, I have th that has the title, and a td that has a category.

What I want to extract is all titles class=title and their corresponding category number class=category for all table rows in all tables. I've loaded the file in $html. Can someone tell me what I'm supposed to find after that? I've tried even $collection = $html->find('tr'); and did a vardump on the collection but got nothing, so it looks like I'm not selecting right.

<table class="sometable">
  <tbody>
    <tr class="sometr">
      <th><a class="title">Table 1 Title1</a></th>
      <td class="category" id="categ-113"></td>
      <td class="somename">Table 1 Title 1 name</td>
    </tr>
    <tr></tr>
    <tr></tr>                           
  </tbody>
</table>

<table class="sometable">
</table>

<table class="sometable">
</table>

1条回答
小情绪 Triste *
2楼-- · 2020-04-01 07:49

I have tested this and it works

$tables = $dom->find('table[@class="sometable"]');

foreach($tables as $table)
{
    $titles = $table->find('a[@class="title"]');
    foreach($titles as $title)
    {
        echo "Link title = " . $title ."<br />";
    }

    $categories = $table->find('td[@class="category"]');
    foreach($categories as $category)
    {
        echo "Category id = " . $category->id ."<br />";
    }

    $titles2 = $table->find('td[@class="somename"]');
    foreach($titles2 as $title2)
    {
        echo "Title2 = " . $title2 ."<br />";
    }

}
查看更多
登录 后发表回答