【问题标题】:Collecting Links from Multiple Pages with QueryPath使用 QueryPath 从多个页面收集链接
【发布时间】:2012-09-12 19:17:19
【问题描述】:

我有一个函数,它传递了一个 url 数组。每个网页都会有一系列指向其他页面的链接。我想从传递给此函数的每个网页返回这些链接的完整列表。我被困在如何在每个循环中组合数组。

 function getitemurls ($pagelinks) {
 global $host;
 foreach($pagelinks as $link) {
   $circdl = my_curl($link);
   $circqp = htmlqp($circdl,'body');
   $circlinks = array();
   foreach ($circqp->branch()->top('area[href]') as $item) {
   $circlinks[] = $item->attr('href');
    }
   for ($i = 0; $i < count($circlinks); ++$i) {
   $fullitemurl = join(array($host,$circlinks[$i]));
   }
    }
  return $fullitemurl;
 }

例如:

 Webpage 1: page1.html
 <html><body><area shape="rect" href="http://www.google.com" coords="110,151,173,225" alt=""/></body></html>

 Webpage 2: page2.html
      <html><body><area shape="rect" href="http://www.yahoo.com" coords="110,151,173,225" alt=""/></body></html>

这是两个页面的数组:

 $array = array (
"0" => "page1.html",
"1" => "page2.html", );

我想从这个数组中返回:

 getitemurls($array)
 Array ( [0] => http://www.google.com [1] => http://www.yahoo.com)

【问题讨论】:

  • 弄清楚:刚刚在循环之前将 $fullitemurl 声明为一个数组。现在效果很好!

标签: php arrays querypath


【解决方案1】:

我最终只是在循环之前声明了我的数组,然后在循环中这样分配它:

 function getitemurls ($pagelinks) {
  global $host;
  $fullitemurls = array();
  foreach($pagelinks as $link) {
   $circdl = my_curl($link);
   $circqp = htmlqp($circdl,'body');
   $circlinks = array();
   foreach ($circqp->branch()->top('area[href]') as $item) {
    $circlinks[] = $item->attr('href');
   }
   for ($i = 0; $i < count($circlinks); ++$i) {
    $fullitemurl[] = join(array($host,$circlinks[$i]));
   }
  }
 return $fullitemurl;
}

【讨论】:

    猜你喜欢
    • 2022-09-29
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2016-01-05
    相关资源
    最近更新 更多