1

タグのsrc属性の内容を取得したい。<img>私が使用しているコードは次のとおりです。

require_once( 'simple_html_dom.php');

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $webpage);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);  
$str = curl_exec($curl);  
curl_close($curl);  

if( $str )
{
    $html= str_get_html($str);
    $img = $html->find('img', 0); // get the first image on the page
    $src = $img->src; // get the contents of the img src - but it doesn't seem to work
}

私は何を間違っていますか?

4

3 に答える 3

2

これを試して:-

<?php
include("simple_html_dom.php");

$webpage ="http://www.santabanta.com";

$html = file_get_html($webpage);

foreach($html->find('img') as $element) {
    echo $element->src . '<br>'; 
}
?>
于 2012-08-03T06:09:52.127 に答える
0

あなたは最初の行の最初を逃しています'!!!

交換:

require_once( simple_html_dom.php');

と:

require_once( 'simple_html_dom.php');
于 2012-08-03T05:09:30.607 に答える
0

PHPが提供するDOMパーサーを使用して、次のように1番目の画像のsrcを取得できます。

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $webpage);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);  
$html = curl_exec($curl);
curl_close($curl);  

if( !empty($html) ) {
   $doc = new DOMDocument;
   libxml_use_internal_errors(true);
   $doc->loadHTML($html);
   #echo $doc->saveHTML();
   $xpath = new DOMXPath($doc);
   $src = $xpath->evaluate("string(//img/@src)");
   echo "src=" . $src . "\n";
}
于 2012-08-03T05:31:50.010 に答える