我想在 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/