如何使用PHP和Simple HTML Dom从表中解析特定部分

时间:2017-05-15 11:32:05

标签: php parsing simple-html-dom

我设法从某个网站解析一个表,但我只需要一些元素:

include('simple_html_dom.php'); 
$html = file_get_html($kako);
foreach($html->find('table') as $e)
  echo $e->innertext . '<br>';
$html->clear();
unset($html);

对于此输入:

<tr>
  <td style="word-wrap: break-word;">
    <a href="/info/......us" title="......">Title</a> 
    <img alt="Verified" Title="Verified and marked" src="/images/verified.png" width="20" height="20">
    <br>
    <a href="/file/........." rel="nofollow">
    <img alt="........" Title="........." src="......" width="16" height="16"></a>
    <a href="magnet:?xt=urn:btih:.......A1337" rel="nofollow">
    <img  alt="Download ..... using magnet link" Title="Dow.....ing magnet link" src="/images/magnet.svg" width="16" height="16"></a> 
    Uploaded 5 days ago Size 1.6 GB</td><td class="is-hidden-touch" >1.6 GB
  </td>
  <td class="is-hidden-touch" style="text-align: center;" >5</td>
  <td class="is-hidden-touch" style="text-align: center;">5 days ago</td><td style="text-align: center;">6627</td><td style="text-align: center;">2445</td>
</tr>

我需要标题,磁铁链接(<a href="magnet:?>)和文字“5天前上传....”。

这可能吗?我搜索了所有手册,但找不到任何东西。

1 个答案:

答案 0 :(得分:0)

foreach($html->find('a') as $element){
        if (strpos($element->href, 'magnet:') !== false){
            $hrf = $element->href;
        }
    }
    foreach($html->find('img') as $ele){
        if(strpos($ele->title, 'magnet link') !== false){
            $title = $ele->title;
        }
    }
    foreach($html->find('td') as $eletd){
        if(strpos($eletd->text,'Uploaded') !== false){
            $text = $eletd->text;
        }
    }

我根据您的HTML自定义代码。希望这对你有用!