mrkatz / laravel-goutte
Laravel-Goutte 是 FriendsOfPHP Goutte 包的 Laravel 服务容器包装器,Goutte 是一个用于 PHP 的屏幕抓取和网页爬取库。
Requires
- fabpot/goutte: ^3.2
- mrkatz/laravel-helpers: dev-master
Requires (Dev)
- orchestra/testbench: ^3.8|^4.0
This package is auto-updated.
Last update: 2024-09-05 10:54:12 UTC
README
Laravel-Goutte 是 FriendsOfPHP Goutte 包的 Laravel 服务容器包装器,Goutte 是一个用于 PHP 的屏幕抓取和网页爬取库。
Goutte 提供了一个优雅的 API 用于爬取网站并从 HTML/XML 响应中提取数据。
要求
Goutte 需要 PHP 7.1+ 和 Guzzle 6+。
安装
在您的 composer.json 文件中添加 mrkatz/laravel-goutte 作为依赖项
.. code-block:: bash
composer require mrkatz/laravel-goutte
使用方法
创建一个 Goutte 客户端实例(它扩展了 Symfony\Component\BrowserKit\Client)
.. code-block:: php
use Goutte\Client;
$client = new Client();
使用 request() 方法进行请求
.. code-block:: php
// Go to the symfony.com website
$crawler = Goutte::request('GET', 'https://www.symfony.com/blog/');
该方法返回一个 Crawler 对象(Symfony\Component\DomCrawler\Crawler)。
如果您想使用自己的 Guzzle 设置,可以为 Goutte 创建并传递一个新的 Guzzle 6 实例。例如,为了添加 60 秒的请求超时
.. code-block:: php
use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;
$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
'timeout' => 60,
));
$goutteClient->setClient($guzzleClient);
点击链接
.. code-block:: php
// Click on the "Security Advisories" link
$link = $crawler->selectLink('Security Advisories')->link();
$crawler = $client->click($link);
提取数据
.. code-block:: php
// Get the latest post in this category and display the titles
$crawler->filter('h2 > a')->each(function ($node) {
print $node->text()."\n";
});
提交表单
.. code-block:: php
$crawler = $client->request('GET', 'https://github.com/');
$crawler = $client->click($crawler->selectLink('Sign in')->link());
$form = $crawler->selectButton('Sign in')->form();
$crawler = $client->submit($form, array('login' => 'fabpot', 'password' => 'xxxxxx'));
$crawler->filter('.flash-error')->each(function ($node) {
print $node->text()."\n";
});
更多信息
阅读 BrowserKit 和 DomCrawler Symfony 组件的文档以获取有关 Goutte 可以做什么的更多信息。
发音
Goutte 发音为 goot,即与 boot 同韵,而不是 out。
技术信息
Laravel-Goutte 是以下优秀的 PHP 库的薄包装器
-
FriendsOfPHP/Goutte
-
Symfony 组件:
BrowserKit、CssSelector和DomCrawler_; -
Guzzle_ HTTP 组件。
许可证
Goutte 采用 MIT 许可证。
.. _Composer: https://composer.php.ac.cn .. _Guzzle: http://docs.guzzlephp.org .. _BrowserKit: https://symfony.com.cn/components/BrowserKit .. _DomCrawler: https://symfony.com.cn/doc/current/components/dom_crawler.html .. _CssSelector: https://symfony.com.cn/doc/current/components/css_selector.html