2017-11-13 5 views
0

src 이미지를주고 싶은이 이미지에는 6 개의 이미지가 있습니다. 내 목표는 PHP로 모든 이미지 src를 가져 오는 중 하나의 이미지 src 만 가져 오는 것입니다.PHP를 사용하여이 URL에있는 기존 이미지 src를 모두 얻는 방법은 무엇입니까?

<?php 
require_once ('simple_html_dom/simple_html_dom.php'); 
$html = file_get_html('https://www.zara.com/tr/en/flatform-derby-shoes-with-reversible-fringe-p15318201.html?v1=5276035&v2=734142'); 
foreach($html->find('img') as $element){ 
    echo $element->src . '<br>'; 
} 
?> 
+2

내 생각 엔 간단한 HTML DOM이 포함되지 않은'src' 년대에 문제가 있다는 것 실제 URL (특별히 버그 추적기를 읽은 후). 이미지 중 하나만 실제로'src'에 URL을 가지고 있습니다. 나머지는'src = "data : image/png; base64, ...."(인라인 이미지)입니다. 대신 PHP의 [DOMDocument] (http://php.net/manual/en/class.domdocument.php)를 사용해 보셨습니까? –

+0

@ MagnusEriksson 당신은 실제 답변으로 제출해야합니다 ... – anarcat

+0

@anarcat 완료. 적절한 코멘트가있는 작업 예제를 포함하기를 원했습니다. –

답변

2

버그 추적기 Simple HTML DOM을 살펴본 후. 실제 URL이 아닌 값을 가져 오는 데 문제가있는 것 같습니다.

가져 오려는 페이지의 소스를 보면 실제로는 하나의 이미지에만 URL이 있습니다. 나머지는 인라인 이미지가 있습니다 : src="data:image/png;base64,...".

나는 이것을 위해 PHP 자신의 DOMDocument을 사용할 것을 제안합니다.

<?php 
// Get the HTML from the URL 
$data = file_get_contents("https://www.zara.com/tr/en/flatform-derby-shoes-with-reversible-fringe-p15318201.html?v1=5276035&v2=734142"); 

$doc = new DOMDocument; 
// DOMDocument throws a bunch of errors since the HTML isn't 100% valid 
// (and for all HTML5-tags) but it will sort them out. 
// Let's just tell it to fix it in silence. 
libxml_use_internal_errors(true); 

$doc->loadHTML($data); 

libxml_clear_errors(); 

// Fetch all img-tags and get the 'src' attributes. 
foreach ($doc->getElementsByTagName('img') as $img) { 
    echo $img->getAttribute('src') . '<br />'; 
} 

데모 :

+1

'PHP Tidy' (DOMDocumnet이 유효하지 않은 HTML을 분류 할 것이므로)가 필요 없다는 것을 지적 해 주신 고맙습니다. –

+0

나는이 URL에 신발의 src 이미지를 제공하지만 신발의 src 이미지는 나오지 않는다. –

+0

@mohammadsaber - HTML에서 이미지를 렌더링하지 않기 때문에 페이지로드시 Javascript/Ajax를 통해 렌더링 할 가능성이 높습니다. 그것은 완전히 다른 매우 복잡한 질문입니다. 이것은 당신이 얻는 모든 이미지를 얻는 것에 관한 것이 었습니다. 그것들 중 5 개 (인라인 이미지 데이터)는 단지 빈 png와 같은 것처럼 보입니다. –

0

https://www.tehplayground.com/sh4yJ8CqIwypwkCa가 실제로 그 base64encodes이 이미지 base64ecnoded images 있습니다

여기 (코멘트) 작업 솔루션입니다. 이미지가 base64로 인코딩되었지만이 페이지를 파싱하려면 이미지의 부모 인 태그에 이미지 URL이 실제로 포함되어 있습니다.

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch,CURLOPT_URL,"https://www.zara.com/tr/en/flatform-derby-shoes-with-reversible-fringe-p15318201.html?v1=5276035&v2=734142"); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
$data = curl_exec($ch); 
curl_close($ch); 

지금은 데이터 조작

libxml_use_internal_errors(true); 
$siteData = new DOMDocument(); 
$siteData->loadHTML($data); 

$a = $siteData->getElementsByTagName("a"); //get the a tags 
for($i=0;$i<$a->length;$i++){ 
    if($a->item($i)->getAttribute("class")=="_seoImg"){ //_seoImg class is the image class 
     echo $a->item($i)->getAttribute("href").'<br/>'; 
    } 
} 

과 결과는

//static.zara.net/photos///2017/I/1/1/p/5318/201/040/3/w/560/5318201040_2_1_1.jpg?ts=1508311623896 
//static.zara.net/photos///2017/I/1/1/p/5318/201/040/3/w/560/5318201040_1_1_1.jpg?ts=1508311816920 
//static.zara.net/photos///2017/I/1/1/p/5318/201/040/3/w/560/5318201040_2_3_1.jpg?ts=1508311715728 
//static.zara.net/photos///2017/I/1/1/p/5318/201/040/3/w/560/5318201040_2_10_1.jpg?ts=1508315639664 
//static.zara.net/photos///2017/I/1/1/p/5318/201/040/3/w/560/5318201040_2_2_1.jpg?ts=1508311682567 
관련 문제