我正在尝试从网站上抓取一些内容。我最终发现它需要 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