我有兴趣尝试网络抓取。但如果我使用下面的代码,我会抛出错误 like((!)致命错误:在非对象上调用成员函数innertext())
include_once('simple_html_dom.php');
set_time_limit(300);
$url = "http://www.flickr.com/photos/terriek/galleries/72157622371738280/";
echo $url;
$ch = curl_init();
echo $ch;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec ($ch);
//echo $result;
curl_close($ch);
$html = new simple_html_dom();
echo $html;
$html->load($result);
$exts = array('jpg', 'jpeg', 'png', 'gif');
foreach($html->find('img') as $element) // error with this line
$path_parts = pathinfo($element->src);
// if condition
$ch = curl_init($element->src);
$fp = fopen("imgs/".$path_parts['basename'], "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
echo curl_exec($ch);
curl_close($ch);
fclose($fp);
答案 0 :(得分:0)
当您在循环中卷曲时,可能是您错过了完整的网址。
尝试:
echo $element->src;
在你的循环中并确保它提供完整的网址,如果它提供了相对网址,请在卷曲前将$ url添加到其中。
答案 1 :(得分:0)
问题是主要网址 - 当您在浏览器中打开它时,您会看到它重定向到安全协议,因此将其更新为https
应该可以使代码正常工作:
$url = "https://www.flickr.com/photos/terriek/galleries/72157622371738280/";