我想从下面的代码中得到值,但是结果是空的。
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Goutte\Client;
class ScrapeController extends Controller
{
private $results = array();
public function scraper(){
$client = new Client();
$raw = $client->request('GET', 'https://shopee.co.id/search?keyword=phone');
$raw->filter('.col-xs-2-4')->each(function ($item) {
$this->results[$item->filter('._1nHzH4')->text()] = $item->filter('_32hnQt')->text();
});
return $this->results;
}
}这段代码不能工作。有人能给我解决办法吗?
发布于 2021-06-16 06:33:40
使用Guzzle客户端从Laravel应用程序发出传出请求。
private $results = array();
public function scraper(){
$client = new Client();
$guzzleClient = new GuzzleClient(array());
$client->setClient($guzzleClient);
//then make request
$raw = $client->request('GET', 'https://shopee.co.id/search?keyword=phone');
$raw->filter('.col-xs-2-4')->each(function ($item) {
$this->results[$item->filter('._1nHzH4')->text()] = $item->filter('_32hnQt')->text();
});
return $this->results;
}发布于 2021-06-16 06:33:43
之所以会发生这种情况,是因为页面的内容是通过javascript加载的,如果您想、刮,那么首先需要使用幻影之类的工具执行javascript,或者使用幻影库https://github.com/jonnnnyw/php-phantomjs。
因此,就您的情况而言,步骤如下:
1:使用幻影get执行javascript并获取原始HTML
2:将HTML传递给goutte,然后过滤数据
https://stackoverflow.com/questions/67997353
复制相似问题