how to scrape this with Simple HTML DOM [closed]

时间秒杀一切 提交于 2019-12-01 13:18:28

问题


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:


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 />";
    }

}


来源:https://stackoverflow.com/questions/7465907/how-to-scrape-this-with-simple-html-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!