我正在尝试使用Simple HTML DOM Parser解析HTML页面。这个HTML页面没有使用ID,这使得更难以引用元素。
在此页面上,我试图获取专辑名称,歌曲标题,下载链接和专辑图像。我已经做到了这一点,但我甚至无法获得专辑名称!
$html = file_get_html('http://music.banadir24.com/singer/aasha_abdoo/247.html');
$article = $html->find('table td[class=title]', 0);
foreach($article as $link){
echo $link;
}
输出:1tdArrayArrayArray Artist Array
我需要得到这种输出:
Image Path
Duniya Jamiila [URL]
Macaan Badnoo [URL]
Donimaayee [URL]
...
感谢大家的帮助
请注意:这是合法的,因为歌曲不受版权约束,可以自由下载,只需要下载很多歌曲,我不能整天坐在那里点击按钮。话虽如此,我花了一个小时才能做到这一点。
答案 0 :(得分:1)
这是你的意思吗?
$urls = $html->find('table[width=100%] table tr');
foreach($urls as $url){
echo $url->children(2);
echo $url->children(6)->children(0)->href;
echo '<br>';
}
修改强>
根据您的评论,这里有一些更新的代码,包含一些(希望)有用的评论。
$urls = $html->find('table[width=100%] table tr');
foreach($urls as $url){
// Check that we actually have the right number of children, this was what was breaking before
if ($url->children(6)) {
/* Without the following check, we get a digg icon and a useless link. You can merge this with the if statement above, I only have it
* seperated so that I can write this comment and it will make more sense when reading it for the first time.
*/
if ($url->children(2)->children(0)->src == 'images/digg.png' || $url->children(2)->children(0)->href == 'javascript:void(0)') continue;
// echo out the name of the artist. You can get the text without the link by using $url->children(2)->plaintext
echo $url->children(2);
// echo out the link. Obviously you could put this href inside a <a href="code-here">whatever-here</a> tag to make the links clickable.
echo $url->children(6)->children(0)->href;
echo '<br>'; // just for readability
}
}
答案 1 :(得分:0)
您的示例中使用的页面上只有三个TD标记具有值为“title”的class属性。
1. <td height="35" class="title" style="padding-left:7px;"> Artist</td>
2. <td colspan="3" height="35" class="title" style="padding-left:7px;"><img src="images/b24/dot_next.png" />Desco</td>
3. <td colspan="3" height="35" class="title" style="padding-left:7px;"><img src="images/b24/dot_next.png" />The Best Of Aasha</td>
第一个始终包含文本“Artist”,另一个包含相册的标题。它们也是唯一具有class =“title”AND colspan =“3”的TD标签,因此使用HTML DOM Parser选择它们应该非常容易。