PHPQuery ...试图从页面获取所有图像的所有链接

时间:2014-01-06 00:57:39

标签: php jquery parsing phpquery

我正在尝试使用PHPQuery获取给定页面上所有图像的所有链接。我正在使用PHPQuery的PHP支持语法。

这是我到目前为止的代码:

include('phpQuery-onefile.php');

$all = phpQuery::newDocumentFileHTML("http://www.mysite.com", $charset = 'utf-8');

// in theory this gives me all image sources
$images = $all->find('img')->attr('src'); 

// but if I do `echo $images;` what I get is the src to the first image

出于好奇,我试过了

$images = $all->find('img:first')->attr('src'); 

$images = $all->find('img:last')->attr('src'); 

它分别正确打印了第一个和最后一个图像的地址,但我怎么能得到所有链接的数组呢?

1 个答案:

答案 0 :(得分:5)

在你的foreach循环中,你需要用$a包裹pq()

例如:

$all = phpQuery::newDocumentFileHTML("http://www.mysite.com", $charset = 'utf-8');

$imgs = $all['img'];

foreach ($imgs as $img) {
    // Note: $img must be used like "pq($img)"
    echo pq($img)->attr('src');
}