我有一个带有几个表的HTML文件,尝试从中提取链接和图像部分。我正在使用PHP简单HTML DOM解析器。
这是要解析的HTML文件:
<h1>Title</h1>
<p>Text</p>
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr><td>
<a href="http://www.google.com/some_url">
<img width="100" height="100" border="0" src="http://google.com/some_image.jpg"/>
</a>
</td></tr>
</tbody>
</table>
<h2>Title</h2>
<p>Text</p>
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr><td>
<a href="http://www.google.com/this_url">
<img width="100" height="100" border="0" src="http://google.com/this_image.jpg"/>
</a>
</td></tr>
</tbody>
</table>
<p>Text</p>
<p>Text</p>
而我需要作为输出:
<a href="http://www.google.com/some_url">
<img width="100" height="100" border="0" src="http://google.com/some_image.jpg"/>
</a>
<a href="http://www.google.com/this_url">
<img width="100" height="100" border="0" src="http://google.com/this_image.jpg"/>
</a>
这是PHP的一部分–但无法按我想要的方式工作...
<?php
// Include the library
include('simple_html_dom.php');
// Retrieve the DOM from a given URL
$html = file_get_html('http://google.com');
// Find all images & links
foreach($html->find('img') as $IMGelement)
foreach($html->find('a') as $Aelement)
echo '<a href="' . $Aelement->href . '"><img src="' . $IMGelement->src . '" /><br>';
?>
我认为您想在标签中找到一个img:
foreach($html->find('a img') as $IMGelement) {
echo '<a href="' . $IMGelement->parent()->href . '"><img src="' .$IMGelement->src .'" /><br>';
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句