使用goutte读取文件/字符串


Using goutte to read from a file / string

我正在使用Goutte制作一个webscraper。

为了进行开发,我保存了一个我想遍历的.html文档(这样我就不会经常向网站发出请求)。到目前为止,我拥有的是:

use Goutte'Client;
$client = new Client();
$html=file_get_contents('test.html');
$crawler = $client->request(null,null,[],[],[],$html);

根据我所知,哪个应该调用Symfony''Component''BrowserKit中的请求,并传入原始主体数据。这是我收到的错误消息:

PHP Fatal error:  Uncaught exception 'GuzzleHttp'Exception'ConnectException' with message 'cURL error 7: Failed to connect to localhost port 80: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in C:'Users'Ally'Sites'scrape'vendor'guzzlehttp'guzzle'src'Handler'CurlFactory.

如果我只是使用DomCrawler,那么使用字符串创建一个爬网程序是非常简单的。(请参见:http://symfony.com/doc/current/components/dom_crawler.html)。我只是不确定如何与古特做同样的事情。

提前谢谢。

您决定使用的工具可以进行真正的http连接,但不适合您想要做的事情。至少是现成的。

选项1:实现您自己的BrowserKit客户端

goutte所做的就是扩展BrowserKit的客户端。它使用Guzzle实现http请求。

实现自己的客户端所需要做的就是扩展Symfony'Component'BrowserKit'Client并提供doRequest()方法:

use Symfony'Component'BrowserKit'Client;
use Symfony'Component'BrowserKit'Request;
use Symfony'Component'BrowserKit'Response;
class FilesystemClient extends Client
{
    /**
     * @param object $request An origin request instance
     *
     * @return object An origin response instance
     */
    protected function doRequest($request)
    {
        $file = $this->getFilePath($request->getUri());
        if (!file_exists($file)) {
            return new Response('Page not found', 404, []);
        }
        $content = file_get_contents($file);
        return new Response($content, 200, []);
    }
    private function getFilePath($uri)
    {
        // convert an uri to a file path to your saved response
        // could be something like this:
        return preg_replace('#[^a-zA-Z_'-'.]#', '_', $uri).'.html';
    }
}
 $client = new FilesystemClient();
 $client->request('GET', '/test');

客户端的request()需要接受真实的URI,因此您需要实现自己的逻辑来将其转换为文件系统位置。

请查看Goutte的客户以了解相关情况。

选项2:实现自定义Guzzle处理程序

由于Goutte使用Guzzle,您可以提供自己的Guzzle处理程序,从文件中加载响应,而不是发出真正的http请求。看看处理程序和中间件文档。

如果您只是在缓存响应以减少http请求,那么Guzzle已经为此提供了支持。

选项3:直接使用DomCrawler

new Crawler(file_get_contents('test.html'))

唯一的缺点是您将失去BrowserKit客户端的一些方便方法,如click()selectLink()