laravel - 如何在Laravel 上使用 GuzzleHttp 提取标签
问题描述
解决方案
您可以使用DOMDocument。例如:
use DOMDocument;
class GuzzleController extends Controller
{
public function Index(){
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://google.com');
$html = (string) $response->getBody();
$dom = new DOMDocument;
$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link){
echo $link->nodeValue;
echo $link->getAttribute('href');
}
}
}
这只是一个例子。理想情况下,您会想要注入这些依赖项。
推荐阅读
- c++ - 是否有一个合理的场景,其中 == 运算符不是 != 的否定
- javascript - 过滤多个数组并将它们中的对象添加到单个数组中
- c# - 文件未使用响应对象下载
- python - 导入文件中的类型提示解析运行 NameError 异常
- javascript - 无法将变量传递给 .getRange()
- python - SVD 后的矩阵重乘
- rust - Rust FFI:具有多个字符串指针的函数;第一个参数具有来自后续字符串指针的聚合值
- zapier - Zapier 触发器响应中的嵌套数组问题
- python-3.8 - Python Dbf 追加到内存索引表失败
- python - 在 crom 中执行时无法让我的 python 脚本工作/播放音频