Help us understand the problem. What is going on with this article?

Goutteでスクレイピングする方法めも

More than 5 years have passed since last update.

単にスクレイピングするだけならSymfony2の機能テストと基本的に同じ

直接URLにアクセスする

$client = new \Goutte\Client();

// GET
$client->request('GET', 'http://example.com/some/url');

// POST
$client->request('POST', 'http://exmaple.com/some/post/url', ['foo' => 'bar'], ['image' => '/var/www/myimage.jpg']);

アクセス先の特定のタグの内容・属性を取得する

$client = new \Goutte\Client();

// $crawlerは\Symfony\Component\DomCrawler\Crawler
$crawler = $client->request('GET', 'http://example.com/some/url');

// <h1>this is header!</h1>タグの内容
$crawler->filter('h1')->text(); // →this is header!

// <h2><a href="./subpage.html">サブタイトル</a></h2>タグのhref属性
$crawler->filter('h2 a')->attr('href'); // →./subpage.html

// <ul><li>test1</li><li>test2</li></ul> 全部のliタグの内容
$lists = $crawler->filter('ul li')->each(function(Crawler $node){
    return $node->text();
});
// → $listsは['test1', 'test2']

// 取得したHTML全体
$crawler->html();

アクセス先にあるリンクをクリックする

$client = new \Goutte\Client();

// まずリンクのあるURLにアクセス
$crawler = $client->request('GET', 'http://example.com/some/url');

// 「ここをクリック」をクリックしてリンク先に遷移
$client->click($crawler->selectLink('ここをクリック')->link());

アクセス先にあるフォームを送信する

$client = new \Goutte\Client();

// まずフォームのあるURLにアクセス
$crawler = $client->request('GET', 'http://example.com/some/form');

// 「ログイン」というボタンクリックで送信されるフォームを選択
$form = $crawler->selectButton('ログイン')->form();
$form['username'] = 'testuser';
$form['password'] = 'mypassword';
$crawler->submit($form);

URLにアクセスしてステータスコードを調べる

$client = new \Goutte\Client();

$client->request('GET', 'http://example.com/good/url');
$client->getResponse()->getStatus(); // →200

$client->request('GET', 'http://example.com/bad/url');
$client->getResponse()->getStatus(); // →404

// $client->getResponse()は\Symfony\Component\BrowserKit\Response

アクセス時に使うUserAgentを設定する

$client = new \Goutte\Client();

// UserAgentを"MyCrawler 1.0"に設定
$client->setHeader('User-Agent', 'MyCrawler 1.0');

$client->request('GET', '...'); // MyCrawler 1.0としてアクセス
$client->request('POST', '...'); // MyCrawler 1.0としてアクセス

// 以後、再度setHeader('User-Agent')で何か違うUAを設定するまでずっとこのまま
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away