是的,我正在构建一个网络爬虫,我的代码中有一段可以翻译成绝对urls,而不是/macbookpro/到http://www.apple.com/macbookpro。但是当我回显我的代码时,它只打印一个结果,这是它看到的第一个链接。我是否必须创建一个数组,因为当我创建数组时,我回显了该数组,并列出了单词“array”
<?php
require_once('simplehtmldom_1_5/simple_html_dom.php');
require_once('url_to_absolute/url_to_absolute.php');
$URL = 'http://www.theqlick.com'; // change it for urls to grab
// grabs the urls from URL
$file = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
$links = url_to_absolute($URL, $theelement->href);
}
echo $links;
?>发布于 2012-09-14 01:12:19
var_dump你的数组,它给你一个文本表示你的对象。它将向您显示数组及其元素。Echo更多的是用于输出字符串。您可以循环您的数组并回显每个元素,但如果您只想查看它,var_dump是答案。
http://www.php.net/manual/en/function.var-dump.php
发布于 2012-09-14 01:13:19
如果您尝试在$links中构建数组,则需要执行以下操作
$links[] = url_to_absolute($URL, $theelement->href);现在,您将在每次循环迭代中覆盖$links的值。
您还应该在foreach循环之前的某个位置对$links = array();进行十进制。
发布于 2012-09-14 01:14:41
<?php
require_once('simplehtmldom_1_5/simple_html_dom.php');
require_once('url_to_absolute/url_to_absolute.php');
$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab
// grabs the urls from URL
$file = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
$links[] = url_to_absolute($URL, $theelement->href);
}
print_r($links);因此,您需要初始化该数组,并使用[]将其添加到该数组中,最后使用合适的内容将其实际打印出来,例如print_r。
https://stackoverflow.com/questions/12411413
复制相似问题