【问题标题】:How to get elements from a page using Simple HTML DOM Parser如何使用 Simple HTML DOM Parser 从页面中获取元素
【发布时间】:2011-02-26 21:24:16
【问题描述】:

我正在尝试使用 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]
...

感谢大家的帮助

请注意:这是合法的,因为歌曲不受版权约束,可以免费下载,只是我需要下载很多,我不能整天坐在那里点击按钮。话虽如此,我花了一个小时才走到这一步。

【问题讨论】:

  • 试试 print_r($link);在您的循环中了解有关数组的更多信息。
  • 如果您想从一个页面下载多个文件,您可能需要查看 Firefox 的“DownThemAll!插件”。对于此类问题非常有用的工具,它需要零编程:)
  • @Kau - 我也使用它,但我希望以一种很好的可排序方式将文件放在目录中。
  • @Emil - 当我使用 print_r 时,它会在 dom 中显示很多元素,很难通过!

标签: php html dom parsing


【解决方案1】:

您在示例中使用的页面上只有三个 TD 标记具有值为“title”的类属性。

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>

第一个总是包含文本“艺术家”,其他的总是包含专辑的标题。它们也是唯一带有 class="title" AND colspan="3" 的 TD 标签,因此使用 HTML DOM Parser 应该很容易选择它们。

【讨论】:

    【解决方案2】:

    这是你的意思吗?

    $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>';
    }
    

    编辑

    使用Simple HTML DOM

    根据您的评论,这里有一些更新的代码,其中包含一些(希望是)有用的 cmets。

    $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
       }
    }
    

    【讨论】:

    • 这正是我的意思,如此简洁!但是我怎样才能让它进入下一张专辑呢?对我来说,它似乎只是停下来抱怨Call to a member function children() on a non-object,因为它为第一张专辑做了最后一首歌?
    • 那是因为$url 之一没有任何孩子(或者可能没有 7 个孩子),所以您需要在调用之前检查它是否真的有效。尝试解决它(如果您将来帮助他人,请发布您的答案),否则如果我明天有时间我会再研究一下。
    猜你喜欢
    • 2016-04-09
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2012-04-08
    • 1970-01-01
    相关资源
    最近更新 更多