php - 网页抓取 Symfony/Panther : Can't get HTML

标签 php laravel windows-subsystem-for-linux symfony-panther

我想在 Laravel 应用程序中使用 symfony panther 包抓取站点。根据文档 https://github.com/symfony/panther#a-polymorphic-feline我无法使用 HttpBrowser也不是 HttpClient类,因为它们不支持 JS。

因此,我尝试使用 ChromClient,它使用本地 chrome 可执行文件和 panther 包附带的 chromedriver 二进制文件。

$client = Client::createChromeClient();
$crawler = $client->request('GET', 'http://example.com');
dd($crawler->html());

不幸的是,我只收到空的默认 chrome 页面作为 HTML:

<html><head></head><body></body></html>

$client 做其他事情的每一种方法或 $crawler -instance 导致错误“无可用节点”。

此外,我尝试了文档 https://github.com/symfony/panther#basic-usage 中的基本示例。 --> 同样的结果。

我在 Windows 上的 WSL 下使用 ubuntu 18.04 服务器并安装了 google-chrome-stable deb 包。这似乎有效,因为安装后不再出现错误“找不到二进制文件”。

我还尝试手动使用 Windows 主机系统的可执行文件,但这只会打开一个空的 CMD 窗口,关闭时总是重新打开。我必须通过 TaskManager 终止进程。

这是因为 Ubuntu 服务器没有任何可用的 x-server 吗?
我该怎么做才能接收任何 HTML?

最佳答案

$client = Client::createChromeClient();
$crawler = $client->request('GET', 'http://example.com');

/**
* Get all Html code of page
*/

$client->getCrawler()->html();

/**
* For example to filter field by ID = AuthenticationBlock and get text
*/

$loginUsername = $client->getCrawler()->filter('#AuthenticationBlock')->text();

关于php - 网页抓取 Symfony/Panther : Can't get HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61665180/

相关文章:

php - `wp-admin` 使用 NGINX 从不同文件夹提供 Laravel 和 WordPress 时无法正常工作

php - Controller 方法未被调用 Laravel

ubuntu -/root/在 Windows 10 Ubuntu 上的位置

php - 适用于 Windows 的 Composer 安装-

php - 在 PHP 中对 DateTime 对象数组进行 USort - 没有错误,数组未排序

php - 查询异常 Laravel : Driver not found

node.js - MongoDB-Memory-Server 使 jest 测试调试卡在 WSL 上

environment-variables - 有没有办法在 wsl 中访问 Windows 环境变量?

php - Laravel 迁移 - 列类型字符串不更新大小

PHP/MySQL : Update int where id = column?