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

Spa*_*Dog 0 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
Run Code Online (Sandbox Code Playgroud)

出于好奇,我尝试过

$images = $all->find('img:first')->attr('src'); 
Run Code Online (Sandbox Code Playgroud)

$images = $all->find('img:last')->attr('src'); 
Run Code Online (Sandbox Code Playgroud)

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

r-s*_*sal 5

在你的foreach循环中,你需要$a用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');
}
Run Code Online (Sandbox Code Playgroud)