2010-06-02 8 views
0

간단한 HTML DOM 파서를 사용하여 HTML 페이지를 구문 분석하려고합니다. 이 HTML 페이지는 ID를 사용하지 않으므로 요소를 참조하기가 더 어렵습니다.간단한 HTML DOM 파서를 사용하여 페이지에서 요소를 가져 오는 방법

이 페이지에서 앨범 이름, 노래 제목, 다운로드 링크 및 앨범 이미지를 얻으려고합니다. 나는 이것을했지만 앨범 이름조차도 얻을 수 없다!

$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] 
... 

감사

참고 :이 노래는 같은 법적 저작권에 구속되지 않고 자유롭게 다운로드 할 수 있습니다. 그 중 많은 파일을 다운로드해야하며 하루 종일 버튼을 클릭 할 수 없습니다. 그것을 말하면서, 그것까지 나를 데려 가기 위해 1 시간 걸렸다.

+0

print_r ($ link); 루프 내부에서 배열에 대해 자세히 배울 수 있습니다. –

+0

페이지에서 여러 파일을 다운로드하려는 경우 Firefox의 "DownThemAll! Plugin"을 살펴볼 수 있습니다. 그러한 문제에 대한 매우 유용한 도구이며 제로 프로그래밍이 필요합니다 :) – 2ndkauboy

+0

@ Kau - 나는 또한 그것을 사용합니다. 그러나 나는 좋은 정렬 방식으로 디렉토리에 파일을 배치하기를 희망했습니다. – user356556

답변

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

편집

Simple HTML DOM 사용.

다음은 사용자 의견에 이어 일부 유용한 코드입니다.

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

그건 정확히 내가 의미하는 바로 그 간결한 것입니다! 하지만 다음 앨범으로 어떻게 가야합니까? 나에게 그것은 첫 번째 앨범의 마지막 노래 이름을 마친 후 '객체가 아닌 개체의 자식()에 대한 호출'을 막고 불평하는 것 같습니다. – user356556

+0

'$ url '중 하나에 자식이 없거나 자식이 7 명이 아니기 때문에 호출을하기 전에 실제로 유효한지 확인해야합니다. 그것을 시도해보십시오 (나중에 다른 사람들을 도울 경우 답을 게시하십시오). 그렇지 않으면 내일 시간이 좀 걸릴 것입니다. –

0

"title"값을 가진 class 속성을 가진 예제에서 사용한 페이지에는 TD 태그가 세 개뿐입니다.

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"및 colspan = "3"을 가진 유일한 TD 태그이기도하므로 HTML DOM Parser를 사용하여 태그를 선택하는 것이 매우 쉽습니다.

관련 문제