【问题标题】:Is it possible to parse JSON with Goutte?是否可以用 Goutte 解析 JSON?
【发布时间】:2013-09-14 09:01:41
【问题描述】:

我正在抓取网站,到目前为止,使用 Goutte 解析 HTML 没有问题。但我需要从网站检索 JSON,并且由于 cookie 管理,我不想使用 file_get_contents() 执行此操作 - 这不起作用。

我可以使用纯 cURL,但在这种情况下,我只想使用 Goutte,不想使用任何其他库。

那么有没有什么方法可以让我通过 Goutte 只解析文本,或者我真的必须用好的旧方法来做到这一点?

/* Sample Code */
$client = new Client();
$crawler = $client->request('foo');
$crawler = $crawler->filter('bar'); // of course not working

谢谢。

【问题讨论】:

    标签: php json html-parsing goutte


    【解决方案1】:

    我也可以通过以下方式获取 JSON:

    $client->getResponse()->getContent()->getContents()
    

    【讨论】:

    • 截至 2021 年:$client->getResponse()->getContent()
    【解决方案2】:

    经过几个小时的搜索,我发现了这一点,只需这样做:

    $client = new Client(); // Goutte Client
    $crawler = $client->request("GET", "http://foo.bar");
    
    $jsonData = $crawler->text();
    

    【讨论】:

      【解决方案3】:

      mitthataydogmus 的解决方案对我不起作用。我创建了一个新类“BetterClient”:

      use Goutte\Client as GoutteClient;
      
      class BetterClient extends GoutteClient
      {
          private $guzzleResponse;
      
          public function getGuzzleResponse() {
              return $this->guzzleResponse;
          }
      
          protected function createResponse($response)
          {
              $this->guzzleResponse = $response;
              return parent::createResponse($response);
          }
      }
      

      用法:

      $client = new BetterClient();
      $request = $client->request('GET', $url);
      $data = $client->getGuzzleResponse()->json();
      

      【讨论】:

        【解决方案4】:

        在 Goutte 库中进行了非常深入的搜索后,我找到了一种方法并想分享。因为 Goutte 是一个非常强大的库,但是文档非常复杂。

        解析 JSON 通过(Goutte > Guzzle)

        只需获取所需的输出页面并将 json 存储到数组中即可。

        $client = new Client(); // Goutte Client
        $request = $client->getClient()->createRequest('GET', 'http://***.json');   
        /* getClient() for taking Guzzle Client */
        
        $response = $request->send(); // Send created request to server
        $data = $response->json(); // Returns PHP Array
        

        使用 Cookie 解析 JSON 通过(Goutte + Guzzle) - 用于身份验证

        发送请求站点的页面之一(主页看起来更好)以获取 cookie,然后使用这些 cookie 进行身份验证。

        $client = new Client(); // Goutte Client
        $crawler = $client->request("GET", "http://foo.bar");
        /* Send request directly and get whole data. It includes cookies from server and 
        it automatically stored in Goutte Client object */
        
        $request = $client->getClient()->createRequest('GET', 'http://foo.bar/baz.json');
        /* getClient() for taking Guzzle Client */
        
        $cookies = $client->getRequest()->getCookies();
        foreach ($cookies as $key => $value) {
           $request->addCookie($key, $value);
        }
        
        /* Get cookies from Goutte Client and add to cookies in Guzzle request */
        
        $response = $request->send(); // Send created request to server
        $data = $response->json(); // Returns PHP Array
        

        我希望它有所帮助。因为我几乎花了 3 天时间来了解 Gouttle 及其组件。

        【讨论】:

        • 我尝试使用您的第一种方法,但我收到错误调用未定义方法 GuzzleHttp\Message\Request::send() Guzzle 没有方法发送和 json。
        • 在我查看 api 之后,这是解决问题的代码$response = $client->getClient()->send($request);
        • $client = new Client(); $guzzleClient = $client->getClient(); $headers = array('Authorization' => 'Bearer' . $accessToken); $request = $guzzleClient->createRequest('GET', $linkedInUrl, array('headers' => $headers)); $stream = $guzzleClient->send($request);
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-07
        • 1970-01-01
        相关资源
        最近更新 更多