giauphan/goutte

简单的PHP网页抓取器

安装: 292

依赖: 2

建议: 0

安全: 0

星星: 1

关注者: 1

分支: 0

开放问题: 0

类型:application

v1.2 2024-03-14 02:30 UTC

This package is auto-updated.

Last update: 2024-09-14 03:43:36 UTC


README

Goutte 是一个用于 PHP 的屏幕抓取和网页抓取库。

Goutte 提供了一个简洁的 API 来抓取网站并从 HTML/XML 响应中提取数据。

警告:此库已弃用。从 v4 版本开始,Goutte 成为了 HttpBrowser 类 的简单代理,该类来自 Symfony BrowserKit 组件。要迁移,请在您的代码中将 Goutte\Client 替换为 Symfony\Component\BrowserKit\HttpBrowser

要求

Goutte 依赖于 PHP 7.1+。

安装

giauphan/goutte 添加到您的 composer.json 文件中的 require 依赖项

composer require giauphan/goutte

使用方法

创建一个 Goutte 客户端实例(它扩展了 Symfony\Component\BrowserKit\HttpBrowser

use Goutte\Client;

$client = new Client();

使用 request() 方法发送请求

// Go to the symfony.com website
$crawler = $client->request('GET', 'https://www.symfony.com/blog/');

此方法返回一个 Crawler 对象(Symfony\Component\DomCrawler\Crawler)。

要使用自己的 HTTP 设置,您可以为 Goutte 创建并传递一个 HttpClient 实例。例如,要添加 60 秒的请求超时

use Goutte\Client;
use Symfony\Component\HttpClient\HttpClient;

$client = new Client(HttpClient::create(['timeout' => 60]));

点击链接

// Click on the "Security Advisories" link
$link = $crawler->selectLink('Security Advisories')->link();
$crawler = $client->click($link);

提取数据

// Get the latest post in this category and display the titles
$crawler->filter('h2 > a')->each(function ($node) {
    print $node->text()."\n";
});

提交表单

$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, ['login' => 'giauphan', 'password' => 'xxxxxx']);
$crawler->filter('.flash-error')->each(function ($node) {
    print $node->text()."\n";
});

更多信息

阅读 BrowserKitDomCrawlerHttpClient 的文档,以获取有关 Goutte 可以做什么的更多信息。

发音

Goutte 发音为 goot,即它与 boot 相押韵,而不是与 out 相押韵。

技术信息

Goutte 是以下 Symfony 组件的薄包装: BrowserKitCssSelectorDomCrawlerHttpClient

许可证

Goutte 在 MIT 许可证下授权。