繁体   English   中英

如何使用PHP获取此url中的所有现有图像src?

[英]How to get all existing images src in this url with PHP?

我要提供src图像的网址中有6张图像。 我的目标是使用PHP获取所有图像src,但只有一个图像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>';
}
?>

在查看了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 />';
}

演示: https//www.tehplayground.com/sh4yJ8CqIwypwkCa

实际上,那些base64encodes是图像base64ecnoded图像 就此页面而言,尽管图像是使用base64编码的,但您仍要解析该页面,而作为图像父级的a标签实际上包含图像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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM