1

サイトからコンテンツをスクレイピングしようとしています。最終的に Cookie が必要であることがわかったので、guzzle Cookie プラグインで解決しました。var_dump を実行してもコンテンツを取得できないため、奇妙ですが、「echo」を実行するとページが表示され、データを取得する動的データ呼び出しがあると思われます。私はガズルでAPIにかなり慣れていますが、これを扱うべきかどうかわかりませんか?、ありがとう

domcrawler を使用すると、エラーが発生します。

コード -

   use Symfony\Bundle\FrameworkBundle\Controller\Controller;

   use Symfony\Component\DomCrawler\Crawler;

   use Guzzle\Http\Client;

   use Guzzle\Plugin\Cookie\CookiePlugin;

   use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;

   $cookiePlugin = new CookiePlugin(new ArrayCookieJar());

     $url =  'http://www.myurl.com';
    // Add the cookie plugin to a client
     $client = new Client();

     $client->get();

    $client->addSubscriber($cookiePlugin);

  // Send the request with no cookies and parse the returned cookies
  $client->get($url)->send();

// Send the request again, noticing that cookies are being sent
  $request = $client->get($url);

  $response = $request->send();

 var_dump($response);
 $crawler = new Crawler($response);

  foreach ($crawler as $domElement) {
  print $domElement->filter('a')->links();
   }

エラー

    Expecting a DOMNodeList or DOMNode instance, an array, a   
  string,        or     null, but got "Guzzle\Http\Message\Response
4

2 に答える 2

4

これを試して:

ガズル5用

$crawler = new Crawler($response->getBody()->getContents());

http://docs.guzzlephp.org/en/latest/http-messages.html#id2 http://docs.guzzlephp.org/en/latest/streams.html#creating-streams

ガズル3用

$crawler = new Crawler($response->getBody());

http://guzzle3.readthedocs.org/http-client/response.html#response-body

アップデート

getContents メソッドを使用した Guzzle 5 の基本的な使い方。

include 'vendor/autoload.php';

use GuzzleHttp\Client;

$client = new Client();
echo $client->get('http://stackoverflow.com')->getBody()->getContents();

残りはドキュメントにあります(Cookieを含む)。

于 2015-04-27T15:15:35.153 に答える
1

クローラー オブジェクトをインスタンス化する と、オブジェクト$crawler = new Crawler($response);のフォーム ベースまたはリンク ベースの機能を使用しようとすると、あらゆる種類の Uri ベースのエラーが発生しCrawlerます。

Crawler次のようにオブジェクトをインスタンス化することをお勧めします。

$crawler = new Symfony\Component\DomCrawler\Crawler(null, $response->getEffectiveUrl());

$crawler->addContent(
    $response->getBody()->__toString(),
    $response->getHeader('Content-Type')
);

これは、 createCrawlerFromContentSymfony\Component\BrowswerKit\Clientメソッド内で行う方法でもあります。はGoutteによって内部的に使用されます。Symfony\Component\Browerkit\Client

于 2015-04-29T14:31:12.913 に答える