mfsousa / goutte
简单的PHP网页爬虫
v1.0.5
2014-01-31 18:02 UTC
Requires
- php: >=5.3.0
- ext-curl: *
- guzzle/http: ~3.1
- symfony/browser-kit: ~2.1
- symfony/css-selector: ~2.1
- symfony/dom-crawler: ~2.1
- symfony/finder: ~2.1
- symfony/process: ~2.1
Requires (Dev)
- guzzle/plugin-history: ~3.1
- guzzle/plugin-mock: ~3.1
This package is not auto-updated.
Last update: 2024-09-24 06:05:25 UTC
README
Goutte是一个PHP的屏幕抓取和网页爬取库。
Goutte提供了一个便捷的API来爬取网站并从HTML/XML响应中提取数据。
要求
Goutte适用于PHP 5.3.3或更高版本。
安装
安装Goutte非常简单。下载Goutte.phar文件即可完成安装!
使用方法
在脚本中引入Goutte phar文件以使用Goutte
require_once '/path/to/goutte.phar';
创建一个Goutte Client实例(它扩展了Symfony\Component\BrowserKit\Client)
use Goutte\Client; $client = new Client();
使用request()方法进行请求
// Go to the symfony.com website $crawler = $client->request('GET', 'http://www.symfony.com/blog/');
该方法返回一个Crawler对象(Symfony\Component\DomCrawler\Crawler)。
点击链接
// 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.post > a')->each(function ($node) { print $node->text()."\n"; });
提交表单
$crawler = $client->request('GET', 'http://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是对以下优秀的PHP库的轻量级封装:
- Symfony组件:BrowserKit, ClassLoader, CssSelector, DomCrawler, Finder, 和 Process;
- Guzzle HTTP组件。
许可证
Goutte遵循MIT许可证。