简单的 HTML Dom 解析器 - 无法在抓取后将所有 href 链接放入同一个数组中


Simple HTML Dom Parser - Can't put all href links into the same array after scraping them

我正在尝试抓取一个带有 2 或 3 个 href 链接的网站,用于下载 pdf。这是网页的格式

<p class="file">
                        <a class="ext-pdf" rel="file" href="http://static-mpc.assaabloy.com/lockwoodfile/Fetchfile.aspx?id=2573&amp;dl=1">Deadbolts Catalogue Section</a>
                        <span class="bdi">(.pdf, 660 kB)</span>
                    </p>

                    <p class="file">
                        <a class="ext-pdf" rel="file" href="http://static-mpc.assaabloy.com/lockwoodfile/Fetchfile.aspx?id=2625&amp;dl=1">Lockwood Home Security Solutions</a>
                        <span class="bdi">(.pdf, 3.7 MB)</span>
                    </p>

                    <p class="file">
                        <a class="ext-pdf" rel="file" href="http://static-mpc.assaabloy.com/lockwoodfile/Fetchfile.aspx?id=3045&amp;dl=1">Lockwood Elements Brochure</a>
                        <span class="bdi">(.pdf, 1.2 MB)</span>
                    </p>

到目前为止,我可以从 DOM 获取链接,但我无法将它们放入同一个数组中。这是我的代码:

foreach ($html->find('a.[class="ext-pdf"]') as $pdfurl) {
   $testarray=array($pdfurl->href);   

    print_r($testarray);
}

这是输出 数组 ( [0] => http://static-mpc.assaabloy.com/lockwoodfile/Fetchfile.aspx?id=2594&dl=1 )数组 ( [0] => http://static-mpc.assaabloy.com/lockwoodfile/Fetchfile.aspx?id=2625&dl=1 )

我做错了什么?谢谢!:)

这是任何想知道的人的解决方案:

foreach ($html->find('a.[class="ext-pdf"]') as $pdfurl) 
$testarray[] = $pdfurl->href."<br>";
{

    print_r($testarray);
}
$testarray[] = $pdfurl->href;

是你应该拥有的。您只是每次都为包含 SAME 变量的 url 的数组提供辅助,因此每次循环迭代都会丢弃您上次设置的变量。