Guzzle/Goutte - 基本抓取 - 将变量传递给请求
Guzzle/Goutte - Basic scrape - passing variable to request
我目前正在使用名为 Goutte. It uses Guzzle 的简单 php 爬虫来执行 http GET
请求。我能够执行抓取操作。但是,我试图 pass/echo filter
中的一个变量,但出现错误 Undefined variable: x
。变量已定义。将变量传递给过滤器的正确方法是什么?
$client = new Goutte\Client();
$crawler = $client->request('GET', 'http://github.com/');
$crawler = $client->click($crawler->selectLink('Sign in')->link());
$form = $crawler->selectButton('Sign in')->form();
$x = "hello";
$crawler = $client->submit($form, array('login' => 'xxxxx', 'password' => 'xxxxx'));
$crawler->filter('.flash-error')->each(function ($node) {
echo $x;
print $node->text() . "\n";
});
尝试:
$crawler->filter('.flash-error')->each(function ($node) use (&$x) {
echo $x;
print $node->text() . "\n";
});
如果您不需要在那个函数中更改它,您可以删除 &
。 For reference to inheriting parent variables inside anonymous functions.
我目前正在使用名为 Goutte. It uses Guzzle 的简单 php 爬虫来执行 http GET
请求。我能够执行抓取操作。但是,我试图 pass/echo filter
中的一个变量,但出现错误 Undefined variable: x
。变量已定义。将变量传递给过滤器的正确方法是什么?
$client = new Goutte\Client();
$crawler = $client->request('GET', 'http://github.com/');
$crawler = $client->click($crawler->selectLink('Sign in')->link());
$form = $crawler->selectButton('Sign in')->form();
$x = "hello";
$crawler = $client->submit($form, array('login' => 'xxxxx', 'password' => 'xxxxx'));
$crawler->filter('.flash-error')->each(function ($node) {
echo $x;
print $node->text() . "\n";
});
尝试:
$crawler->filter('.flash-error')->each(function ($node) use (&$x) {
echo $x;
print $node->text() . "\n";
});
如果您不需要在那个函数中更改它,您可以删除 &
。 For reference to inheriting parent variables inside anonymous functions.