2014-01-15 3 views
0

웹 사이트 목록에서 html을 구문 분석하여 제품 배열을 제공하는 스크립트를 작성하고 있습니다.구문 분석하여 그림 URL을 가져올 수 없습니다.

나는 임 잘 모든 일을 믿고 ...하지만 어떤 이유로 나는 임 요소를 검색하는 DOMXPath를 사용하여 하나 개의 웹 사이트

Makita.ca

그래서 ..와 어려움의 alots 있습니다. 내가 얻고 싶은 어떤 사진

가 왼쪽

에있는 사진에 그입니다 makita.ca에서 점점 메신저 원시 HTML을 제공하고

하시기 바랍니다 또한 내가 필요로하는 유일한 것은의 링크이므로주의 이미지가 아니라 실제 이미지가 입니다. 당신은 내 실수가 무엇을 볼 수 있나요

folowing 이미지 페이지는 http://www.makita.ca/index2.php?event=tool&id=100enter image description here

$productArray = array(); 
    $Dom = new DOMDocument(); 
    @$Dom -> loadHTML($this->html); 
    $xpath = new DOMXPath($Dom); 
    echo $xpath -> query('//*[@id="content_other"]/table[2]/tbody/tr/td[1]/table/tbody/tr[4]/td/table/tbody/tr[1]/td/div/a/img')->length; 
     if($xpath -> query('//*[@id="content_other"]/table[2]/tbody/tr/td[1]/table/tbody/tr[4]/td/table')->length > 0) 
     { 
      for($i=0;$i<$xpath->query('//*[@id="content_other"]/table[2]/tbody/tr/td[1]/table/tbody/tr[4]/td/table/tbody/tr')->length;$i++) 
      { 
       if($xpath->query('//*[@id="content_other"]/table[2]/tr/td[1]/table/tr[4]/td/table/tr['.$i.']/td/div/a/img') > 0) 
        $productArray['picture'][] = $xpath -> query('//*[@id="content_other"]/table[2]/tr/td[1]/table/tr[4]/td/table/tr['.$i.']/td/div/a/img')->item(0)->nodeValue; 
      } 
     } 

에있다? 이후 지금은 정말로 잃어 버렸습니다.

편집 : 많은 요소가 '그들이 할 수 있도록

그래서 내가 아래로 구멍 쿼리에 다시 입력 쿼리와 일치하는 방법

내가 쿼리의 길이를 반향하고 테스트 용 확인

() 메소드 마녀가 나에게 제공해야 t가 아닌 asci 문자가 있습니다 그래서 홀 쿼리를 다시 입력했습니다. // // [@ id = "content_other"]/table [2] // tr/td 1/table // tr [4]/td/table // tr 1/td/div/a/img ' 그러면 결과는 0입니다.

그래서 부분적으로 쿼리 부분을 제거했습니다.

//*[@id="content_other"]/table[2]//tr/td[1]/table//tr[4]/td/table//tr[1]/td/div‌​/a = 0 
//*[@id="content_other"]/table[2]//tr/td[1]/table//tr[4]/td/table//tr[1]/td/div‌​ = 0 
//*[@id="content_other"]/table[2]//tr/td[1]/table//tr[4]/td/table//tr[1]/td = 0 
//*[@id="content_other"]/table[2]//tr/td[1]/table//tr[4]/td/table//tr[1] = 0 
//*[@id="content_other"]/table[2]//tr/td[1]/table//tr[4]/td/table = 0 
//*[@id="content_other"]/table[2]//tr/td[1]/table//tr[4]/td = 0 
//*[@id="content_other"]/table[2]//tr/td[1]/table//tr = 5 

여기 일부 요소가 일치합니다. 확인 마지막 요소 마녀를 시도하자가 제로 내가 경로이

//*[@id="content_other"]/table[2]//tr/td[1]/table//tr[4] 

으로 입력해야 TR 번호 5를 얻기 위해 다음을 기반으로하지만 여전히 0를 얻을 수있다 그래서 때문에 내가 필요 하나입니다 ... 그래서 더 이상 무엇을 해야할지 모르겠다.

+2

이 모든'/ tbody' 단계를 제거 정확한 중복입니다에게이 될 것입니다 당신이해야 할 모든 것입니다. 자세한 내용은 주어진 참고 자료를 참조하십시오. –

+0

@JensErat 이봐, 고마워. 하지만 tbody 제거하고 여전히 그것을 작동시킬 수 없습니다. 내가 방금 편집 한 에코 $ xpath -> 쿼리 ('// * [@ id = "content_other"]/table [2] // tr/td [1]/table // tr [4]/td/table// tr [1]/td/div/a/img ') -> 길이; 그리고 그것은 0을 echoing –

+1

나는 엉망이 뭔지 모르겠지만, 마지막 네 축 단계의 문자 중 하나는 ascii가 아니며 쿼리를 중단합니다./table [2] // tr/td [1]/table // tr [4]/td/table // tr [1]/td/div/a/img' (나는 네 단계를 다시 타이핑했다). –

답변

0

은 "행동 샷"을 먼저 선택한 다음이 블록 아래에있는 모든 이미지를 선택합니다.

이 XPath 표현식은 마크 업이 변경 될 때 쉽게 손상 될 수있는 위치 표현식의 수가 적기 때문에 사용자의 것보다 더 신뢰할 수 있습니다.

//div[@class='product_heading']/ancestor-or-self::table[1]//a[@rel='thumbnail']/img

는 강력한 보안

+0

감사! 첫 번째 xpath가 작동합니다. 나는 다르게 다시 작성하는 것에 대해 생각해 본 적이없는 xpath에 의해 너무 많이 점령되었습니다 ... Thanks Again –

+0

두 번째 xpath에서 잘못 배치 된 @ 문자 – Grooveek

관련 문제