【问题标题】:Guzzle response can't be used with Domcrawler()Guzzle 响应不能与 Domcrawler() 一起使用
【发布时间】:2015-04-27 14:47:28
【问题描述】:

我正在尝试从网站上抓取一些内容。我最终发现它需要 cookie,所以我用 guzzle cookie 插件解决了这个问题。这很奇怪,因为我无法通过 var_dump 获取内容,但如果我执行“echo”,它会显示页面,这让我觉得有一些动态数据调用可以获取数据。我已经习惯了使用 guzzle 的 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

【问题讨论】:

    标签: symfony screen-scraping guzzle domcrawler


    【解决方案1】:

    试试这个:

    For Guzzle 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

    For Guzzle 3

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

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

    更新

    Guzzle 5 与 getContents 方法的基本用法。

    include 'vendor/autoload.php';
    
    use GuzzleHttp\Client;
    
    $client = new Client();
    echo $client->get('http://stackoverflow.com')->getBody()->getContents();
    

    其余的在doc(包括cookie)。

    【讨论】:

    • 它无法识别 getContents
    • 我收到以下错误 - 尝试在“Guzzle\Http\EntityBody”类上调用方法“getContents”
    • 您可能使用的是过时的 Guzzle 版本 3。尝试只调用getBody() 方法或$response->getBody()->__toString()
    • 不,我用的是最新的。
    • 相信我,你不会。 Guzzle\Http\EntityBody 类在版本 3 中,现已弃用。最新版本是5.2.0。你试过我最后的建议了吗?
    【解决方案2】:

    如果您实例化像$crawler = new Crawler($response); 这样的爬虫对象,当您尝试使用Crawler 对象的任何基于表单或链接的函数/特性时,您将收到各种基于Uri 的错误。

    我建议实例化您的 Crawler 对象,例如:

    $crawler = new Symfony\Component\DomCrawler\Crawler(null, $response->getEffectiveUrl());
    
    $crawler->addContent(
        $response->getBody()->__toString(),
        $response->getHeader('Content-Type')
    );
    

    这也是Symfony\Component\BrowswerKit\ClientcreateCrawlerFromContent 方法中的做法。 Symfony\Component\Browerkit\ClientGoutte 在内部使用。

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2015-10-01
      • 2017-09-14
      • 2015-10-06
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多