armonia-tech / goutte
简单的PHP网页爬虫
v3.3.0
2019-03-29 02:58 UTC
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ^6.0
- symfony/browser-kit: ~2.1|~3.0|~4.0
- symfony/css-selector: ~2.1|~3.0|~4.0
- symfony/dom-crawler: ~2.1|~3.0|~4.0
Requires (Dev)
- symfony/phpunit-bridge: ^3.3 || ^4
This package is auto-updated.
Last update: 2024-09-29 05:28:19 UTC
README
这是Armonia Tech的分叉项目,用于解决移动用户代理问题。
安装
composer require armonia-tech/goutte
用法
参考以下内容
Goutte,一个简单的PHP网页爬虫
Goutte是一个用于PHP的屏幕抓取和网页爬虫库。
Goutte提供了一个优秀的API,用于爬取网站并从HTML/XML响应中提取数据。
要求
Goutte依赖于PHP 5.5+和Guzzle 6+。
提示
如果您需要PHP 5.4或Guzzle 4-5的支持,请使用Goutte 2.x(最新phar)。
如果您需要PHP 5.3或Guzzle 3的支持,请使用Goutte 1.x(最新phar)。
安装
在您的composer.json
文件中将fabpot/goutte
添加为require依赖项
composer require fabpot/goutte
用法
创建一个Goutte客户端实例(它扩展了Symfony\Component\BrowserKit\Client
)
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
)。
要使用自己的Guzzle设置,您可能需要创建一个新实例并将其传递给Goutte。例如,要添加60秒请求超时
use Goutte\Client; use GuzzleHttp\Client as GuzzleClient; $goutteClient = new Client(); $guzzleClient = new GuzzleClient(array( 'timeout' => 60, )); $goutteClient->setClient($guzzleClient);
点击链接
// 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, array('login' => 'fabpot', 'password' => 'xxxxxx')); $crawler->filter('.flash-error')->each(function ($node) { print $node->text()."\n"; });
更多信息
阅读BrowserKit和DomCrawler Symfony组件的文档,了解更多关于Goutte可以做什么。
发音
Goutte发音为goot
,即与boot
押韵,而不是与out
押韵。
技术信息
Goutte是对以下优秀的PHP库的薄包装
- Symfony组件: BrowserKit、CssSelector和DomCrawler;
- Guzzle HTTP组件。
许可证
Goutte遵循MIT许可证。