サイトからコンテンツをスクレイピングしようとしています。最終的に 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