Php 如何使用cURL解析图像?
如何使用cURL解析此站点上的图像 使用此代码,我可以显示整个网站的html,但我只需要图像:Php 如何使用cURL解析图像?,php,parsing,curl,Php,Parsing,Curl,如何使用cURL解析此站点上的图像 使用此代码,我可以显示整个网站的html,但我只需要图像: $ch = curl_init('http://www.lamoda.ru/shoes/sapogi/?sitelink=leftmenu&sf=16&rdr565=1#sf=16'); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1'); $tex
$ch = curl_init('http://www.lamoda.ru/shoes/sapogi/?sitelink=leftmenu&sf=16&rdr565=1#sf=16');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
$text = curl_exec($ch);
curl_close($ch);
if (!preg_match('/src="https?:\/\/"/', $text))
$text = preg_replace('/src="(.*)"/', "src=\"$MY_BASE_URL\\1\"", $text);
echo $text;
谢谢大家!
我试过这个:
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
$text = curl_exec($ch);
curl_close($ch);
$doc = new DOMDocument();
@$doc->loadHTML($text->content);
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img)
{
$imgarray[] = $img -> getAttribute('src');
}
return $imgarray;
但是:在这个网站上,通过JS上传的图像根本不显示图像=((您可以使用DOM解析器来实现这一点:
$ch = curl_init('URL_GOES_HERE');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
$text = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument;
$dom->loadHTML($text);
foreach ($dom->getElementsByTagName('img') as $img) {
echo $img->getAttribute('src');
}
您可以使用html解析简单的html\U dom:
//从URL或文件创建DOM
$url='';
$html=file\u get\u html($url);
//查找所有图像
foreach($html->find('img')as$element)
echo$element->src;
只需使用简单dom解析器之类的工具来评估返回的cURL页面的内容,然后您就可以轻松地对其进行迭代,如$html=new simple\u html\u dom();$html->load($cURL\u scraped\u page,true,false);
然后将foreach($html->find('img')作为$img)://$img=image-endforeach;`您已经完成了。@哦,为什么,没有,有图像是通过jsp上传的,您有一个错误“$dom->loadHTML($text->content);”