我是php新手,从以下php代码生成的数组中提取一些字段时遇到了一个典型问题:
$html=file_get_html("http://www.flipkart.com/books/9781846467622");
$e = $html->getElementById("mprodimg-id");
echo $e;
$f = $e->find('img');
echo $f['data-src'];
在代码中应用var_dump($ f)后看到的输出数组f如下: -
阵 ( [0] => simple_html_dom_node对象 ( [nodetype] => 1 [tag] => IMG [attr] =>排列 ( [onerror] => img_onerror(本); [data-error-url] => http://img1a.flixcart.com/img/book.jpg [height] => 275 [width] => 275 [data-src] => http://img7a.flixcart.com/img/622/9781846467622.jpg [src] =>数据:图像/ GIF; BASE64,R0lGODlhAQABAJEAAAAAAP //// Ly8v /// yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw == [onload] => lzld(本) [alt] =>买数字 [title] =>数字 ) [children] =>排列 ( ) [nodes] =>排列 ( )
我需要回显“data-src”字段的值。有人可以帮忙。
萤火虫看到的html如下所示,我需要抓住它。
<div id="mprodimg-id" class="mprodimg">
<img width="275" height="275" title="Numbers" alt="Buy Numbers" onload="lzld(this)" src="data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP////Ly8v///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==" data-src="http://img7a.flixcart.com/img/622/9781846467622.jpg" data-error-url="http://img1a.flixcart.com/img/book.jpg" onerror="img_onerror(this);">
</div>
答案 0 :(得分:2)
这是一个干净的工作示例:
// includes Simple HTML DOM Parser
include "simple_html_dom.php";
$url = "http://www.flipkart.com/numbers/p/9781846467622?pid=9781846467622";
//Create a DOM object
$dom = new simple_html_dom();
// Load HTML from url
$dom->load_file($url);
// Find the wanted image using the appropriate selectors
$img = $dom->find('#mprodimg-id img', 0);
// Find succeeded
if ($img){
echo $img->{'data-src'};
}
else
echo "Find function failed !";
// Clear DOM object (needed essentially when using many)
$dom->clear();
unset($dom);
OUTPUT
======
http://img7a.flixcart.com/img/622/9781846467622.jpg