README.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. Goutte, a simple PHP Web Scraper
  2. ================================
  3. Goutte is a screen scraping and web crawling library for PHP.
  4. Goutte provides a nice API to crawl websites and extract data from the HTML/XML
  5. responses.
  6. Requirements
  7. ------------
  8. Goutte depends on PHP 5.5+ and Guzzle 6+.
  9. .. tip::
  10. If you need support for PHP 5.4 or Guzzle 4-5, use Goutte 2.x (latest `phar
  11. <https://github.com/FriendsOfPHP/Goutte/releases/download/v2.0.4/goutte-v2.0.4.phar>`_).
  12. If you need support for PHP 5.3 or Guzzle 3, use Goutte 1.x (latest `phar
  13. <https://github.com/FriendsOfPHP/Goutte/releases/download/v1.0.7/goutte-v1.0.7.phar>`_).
  14. Installation
  15. ------------
  16. Add ``fabpot/goutte`` as a require dependency in your ``composer.json`` file:
  17. .. code-block:: bash
  18. composer require fabpot/goutte
  19. Usage
  20. -----
  21. Create a Goutte Client instance (which extends
  22. ``Symfony\Component\BrowserKit\Client``):
  23. .. code-block:: php
  24. use Goutte\Client;
  25. $client = new Client();
  26. Make requests with the ``request()`` method:
  27. .. code-block:: php
  28. // Go to the symfony.com website
  29. $crawler = $client->request('GET', 'https://www.symfony.com/blog/');
  30. The method returns a ``Crawler`` object
  31. (``Symfony\Component\DomCrawler\Crawler``).
  32. To use your own Guzzle settings, you may create and pass a new Guzzle 6
  33. instance to Goutte. For example, to add a 60 second request timeout:
  34. .. code-block:: php
  35. use Goutte\Client;
  36. use GuzzleHttp\Client as GuzzleClient;
  37. $goutteClient = new Client();
  38. $guzzleClient = new GuzzleClient(array(
  39. 'timeout' => 60,
  40. ));
  41. $goutteClient->setClient($guzzleClient);
  42. Click on links:
  43. .. code-block:: php
  44. // Click on the "Security Advisories" link
  45. $link = $crawler->selectLink('Security Advisories')->link();
  46. $crawler = $client->click($link);
  47. Extract data:
  48. .. code-block:: php
  49. // Get the latest post in this category and display the titles
  50. $crawler->filter('h2 > a')->each(function ($node) {
  51. print $node->text()."\n";
  52. });
  53. Submit forms:
  54. .. code-block:: php
  55. $crawler = $client->request('GET', 'https://github.com/');
  56. $crawler = $client->click($crawler->selectLink('Sign in')->link());
  57. $form = $crawler->selectButton('Sign in')->form();
  58. $crawler = $client->submit($form, array('login' => 'fabpot', 'password' => 'xxxxxx'));
  59. $crawler->filter('.flash-error')->each(function ($node) {
  60. print $node->text()."\n";
  61. });
  62. More Information
  63. ----------------
  64. Read the documentation of the `BrowserKit`_ and `DomCrawler`_ Symfony
  65. Components for more information about what you can do with Goutte.
  66. Pronunciation
  67. -------------
  68. Goutte is pronounced ``goot`` i.e. it rhymes with ``boot`` and not ``out``.
  69. Technical Information
  70. ---------------------
  71. Goutte is a thin wrapper around the following fine PHP libraries:
  72. * Symfony Components: `BrowserKit`_, `CssSelector`_ and `DomCrawler`_;
  73. * `Guzzle`_ HTTP Component.
  74. License
  75. -------
  76. Goutte is licensed under the MIT license.
  77. .. _`Composer`: https://getcomposer.org
  78. .. _`Guzzle`: http://docs.guzzlephp.org
  79. .. _`BrowserKit`: https://symfony.com/components/BrowserKit
  80. .. _`DomCrawler`: https://symfony.com/doc/current/components/dom_crawler.html
  81. .. _`CssSelector`: https://symfony.com/doc/current/components/css_selector.html