我尝试抓取此页面: http://hea.uum.edu.my/index.php/academic/current-student/convocation
这是我的代码
<?php
require_once 'vendor/autoload.php';
use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://hea.uum.edu.my/index.php/academic/current-student/convocation');
$step = array();
$i = 0;
$crawler->filter('.sppb-addon.sppb-addon-accordion')->each(function ($node) {
global $step, $i;
$step[$i]['item'] = array();
$node->filter('.sppb-addon-title')->each(function ($node) {
global $step, $i;
$step[$i]['cat'] = $node->html();
});
$j = 0;
$node->filter('.sppb-panel-heading > .sppb-panel-title')->each(function ($node) {
global $step, $i, $j;
$step[$i]['item'][$j++]['title'] = $node->html();
});
$h = 0;
$node->filter('.sppb-panel-body .sppb-addon-content')->each(function ($node) {
global $step, $i, $h;
$step[$i]['item'][$h++]['content'] = $node->html();
});
$i++;
});
print_r($step);
这几乎是完美的,除了 item 的第一个元素没有编号,并且在新数组中编号不会重置。
Array
(
[0] => Array
(
[item] => Array
(
[] => Array //here no number
(
[title] => STEP 1 : ...
[content] => <p>If you are eligible to graduate...
...
[1] => Array
(
[item] => Array
(
[13] => Array //here the number should be 0
(
[title] => STEP 14 : CONVOCATION DRESS ..
[content] => <p>Here are the official...
您可以在此处查看结果:view-source:http://convo18.uum.my/
请帮助。而且,除了解决我的问题之外,我想知道您是否对这种情况有任何优雅的解决方案。
感谢您的时间。
================================================ ========================
更新:感谢@NigelRen的建议,下面的代码有效:
<?php
require_once 'vendor/autoload.php';
use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://hea.uum.edu.my/index.php/academic/current-student/convocation');
$step = array();
$i = 0;
$crawler->filter('.sppb-addon.sppb-addon-accordion')->each(function ($node) use (&$step, &$i) {
$step[$i]['item'] = array();
$node->filter('.sppb-addon-title')->each(function ($node) use (&$step, &$i) {
$step[$i]['cat'] = $node->html();
});
$h = 0;
$node->filter('.sppb-panel-heading > .sppb-panel-title')->each(function ($node) use (&$step, &$i, &$h) {
$step[$i]['item'][$h++]['title'] = $node->html();
});
$h = 0;
$node->filter('.sppb-panel-body .sppb-addon-content')->each(function ($node) use (&$step, &$i, &$h) {
$step[$i]['item'][$h++]['content'] = $node->html();
});
$i++;
});
print_r($step);
答案 0 :(得分:0)
仅测试了一个虚拟设置,我认为解决方案是在任何嵌套函数之外定义$j
和$h
。原因是它们不是在全局范围内定义的,因此,当您说global $step, $i, $j;
然后再说$j++
时,这将是第一次将其定义为undefined,然后发布增量将其设置为1。代码显示这是...
$a = function() {
global $c;
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
输出...
Value=
Value=1
而...
$c=0;
$a = function() {
global $c;
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
提供所需的输出...
Value=0
Value=1
因此,请在开始时定义所有这些内容...
$i = 0;
$j = 0;
$h = 0;
编辑:
尽管根据我的原始评论,global
通常不被接受,但它使测试更加困难,并且(发现)可能无法按您期望的那样工作。建议的方法是使用function(...) use(...) {
方法格式,因此在示例中...
$c=0;
$a = function() use (&$c) {
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();