尝试使用Simple Html Dom Parser获取信息

时间:2014-02-28 15:21:27

标签: php simple-html-dom

我需要你的帮助!两天来我没有成功地撞到砖墙上。 我是PHP的新手(我几个月前开始)。

我使用简单的HTML Dom Parser 类从网页获取一些信息:

1°)“library-”之后的数字。 示例:199459

加上一系列链接:

2°)“/ index.php / link-to-get-878220”

我尝试以标签方式获取这些信息:

    Array
    (
    [Movies] => Array
    (
    [199459] => /index.php/link-to-get-878219
    [199459] => /index.php/link-to-get-878220
    [198210] => /index.php/link-to-get-878452
    [198210] => /index.php/link-to-get-878453
    [198210] => /index.php/link-to-get-878454
    [198210] => /index.php/link-to-get-878455
    )
    )

正如你所看到的,每个数字(即:198210)是几个值的关键(div中包含id =“library - ??????”的所有值都具有相同的键)

问题:

  • 当我试图获得号码时,我得到了一些浪费(即:movies_1)。
  • 我没有成功地在数组中并行数字和链接(这就是我没有在代码中添加示例的原因)

英语不是我的母语,所以请不要犹豫,询问我用过的一些词语!

我的代码:

include('simple_html_dom.php');$movies = array();
$html = new simple_html_dom();
$html->load_file("pt.html");

foreach($html->find('div.libraries div[id*=library-]') as $library):
$movies['idMovie'][] = str_replace('library-', '', $library->id);
foreach($html->find('.list-movies tr') as $links):
$movies['links'][] = $links->find('.nom a',0)->href;
endforeach;
endforeach;

我尝试从以下网址获取信息:

<div class="libraries library_movie-tabs red tabs">
<div class="libraries library_element-tabs red tabs">
<div id="library-199459" class="tabs-content">
<div class="inner">
<p class="info"><strong>Library 1:</strong>Fantasy Movies</p>
</div>

<div class="inner">
<table width="100%" class="library-table list-movies">
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Conan"
href= "/index.php/link-to-get-878219"></td>
</tr>

<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Lord-Of-The-Ring" 
href= "/index.php/link-to-get-878220"></td>
</tr>
</table>
</div>
<div id="library-movies_1" class="library-movies">
...
</div> 
</div>

<div id="library-198210" class="tabs-content">
<div class="inner">
<p class="info"><strong>Library 2 :</strong>S-F Movies</p>
</div>

<div class="inner">
<table width="100%" class="library-table list-movies">
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Tron" 
href= "/index.php/link-to-get-878452"></td>
</tr>
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Starwars" 
href= "/index.php/link-to-get-878453"></td>
</tr>
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Star-Trek" 
href= "/index.php/link-to-get-878454"></td>
</tr>
<tr>
<td width="40"></td>

<td width="228" class="nom"><a title="Predator" 
href= "/index.php/link-to-get-878455"></td>
</tr>
</table>
</div>
<div id="library-movies_1" class="library-movies">
...
</div> 
</div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

您的问题是内部foreach想要迭代$library,mot $html

foreach($html->find('.tabs-content') as $library){
  $item = array('links' => array());
  $key = str_replace('library-', '', $library->id);
  foreach($library->find('a') as $a){
    $item['links'][] = $a->href;
  }
  $movies[$key] = $item;
}
相关问题