我正在抓取一个网站获取一些数据,这是一段时间的工作,所以我用Google搜索并发现Queue对这个过程有好处我被困在这个错误中
Serialization of 'Closure' is not allowed
我的代码:
class SiteScraper extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $client;
protected $crawler;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
$this->client = new Client();
$this->crawler = $this->client->request('GET', 'example.com/login/');
$form = $this->crawler->selectButton('Log In')->form();
$this->crawler = $this->client->submit($form, array('email' => 'useremail', 'pass' => 'pass'));
$this->crawler->filter('.flash-error')->each(function ($node) {
print $node->text() . "\n";
});
}
public function handle()
{
$crawler = $this->client->request('GET', $url_to_traverse);
$status_code = $this->client->getResponse()->getStatus();
if($status_code == 200){
$crawler->filter('.friendBrowserNameTitle > a')->each(function ($node) {
$names = $node->text() . '<br>';
$profileurl = $node->attr('href') . '<br>';
echo "Name : " . $names . " Profile Link : " . $profileurl;
});
}
else{
echo $status_code;
}
}
}
我出错的任何帮助?
答案 0 :(得分:3)
在处理作业时,只有Eloquent模型才会被优雅地序列化和反序列化(Source)
所以我想在你的情况下,你必须将你当前的构造代码写入 handle()方法
class SiteScraper extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(){ }
public function handle()
{
$client = new Client();
$crawler = $client->request('GET', 'example.com/login/');
$form = $crawler->selectButton('Log In')->form();
$crawler = $client->submit($form, array('email' => 'useremail', 'pass' => 'pass'));
$crawler->filter('.flash-error')->each(function ($node) {
print $node->text() . "\n";
});
$crawler = $client->request('GET', $url_to_traverse);
$status_code = $client->getResponse()->getStatus();
if($status_code == 200){
$crawler->filter('.friendBrowserNameTitle > a')->each(function ($node) {
$names = $node->text() . '<br>';
$profileurl = $node->attr('href') . '<br>';
echo "Name : " . $names . " Profile Link : " . $profileurl;
});
}
else{
echo $status_code;
}
}
}