我怎么能用cURL解析图像?

时间:2013-12-01 07:01:15

标签: 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');


  $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上传的图片并没有显示图像=((

2 个答案:

答案 0 :(得分:2)

您可以使用DOM Parser来实现此目的:

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

答案 1 :(得分:0)

你可以使用html解析simple_html_dom:
http://simplehtmldom.sourceforge.net/manual.htm
// Create DOM from URL or file
$url = 'http://www.lamoda.ru/shoes/sapogi/?sitelink=leftmenu&sf=16&rdr565=1#sf=16'; $html = file_get_html($url);
// Find all images
foreach($html->find('img') as $element)
echo $element->src;