php - Laravel 返回 UnexpectedValueException
问题描述
我在 Laravel 中的抓取功能遇到了一些问题。该函数正在返回该抓取数据的值,但下面显示了类似的消息。
UnexpectedValueException: The Response content must be a string or object implementing __toString()
我在这里做错了什么?
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Goutte;
class ScraperController extends Controller
{
public function index(){
$price = $this->getPrice();
return $price;
}
public function getPrice(){
$final_price = '';
$crawler = Goutte::request('GET', 'https://www.aliexpress.com/item/Cable-Chompers-Animal-Protectors-Bite-Cable-Bite-Protector-Saver-For-Iphone-USB-Charger-Cable-Cute-Cartoon/32917115384.html');
$crawler->filter('#j-sku-price')->each(function ($node) {
$price = $node->text();
print($price);
});
return true;
}
}
解决方案
$price
索引函数中的变量采用布尔值。在你的getPrice()
你必须返回一个string
(或对象)而不是boolean
. 例如:
public function getPrice(){
// ...
return $final_price; // if the value of $final_price is what you want
}
推荐阅读
- python - np.where 用字符串创建缺失值
- c# - httpwebrequest 在 chrome 上工作而不是在 c# 上?目标机器拒绝连接
- javascript - 在 gulp 4 任务、管道或命令行之间传递数据
- iis - IIS 应用程序池专用内存在启动期间会有所不同
- julia - 朱莉娅:UndefVarError:未定义randperm
- postgresql - 使用临时表的函数的物化视图
- go - Prometheus Custom Registry not working
- angular - Assign inner class function variable to class variable
- jmeter - Unable to test mobile application using Test Script Recorder
- css - R Shiny 编辑标题