简体   繁体   English

Symfony GuzzleHttp Crawler 找不到页面上的所有元素

[英]Symfony GuzzleHttp Crawler cannot find all elements on the page

I am trying to get data from the amazon product page.我正在尝试从亚马逊产品页面获取数据。 But the search for the price always result in The current node list is empty.但是搜索价格总是导致The current node list is empty. . . The other elements like title are present in the result.结果中存在其他元素,如标题。

            $jar = new CookieJar();
            $url = "https://www.amazon.com/Simple-Mobile-Prepaid-Midnight-Carrier/dp/B07XSQT4Q3";
            $client = new Client([
                'cookies' => $jar,
                'headers' => [
                    'Host' => 'www.amazon.com',
                    'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.3',
                    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                    'Accept-Language' => 'en-US,en;q=0.5',
                    'Accept-Encoding' => 'gzip, deflate, br',
                    'Connection' => 'keep-alive',
                    'Upgrade-Insecure-Requests' => '1',
                    'Cache-Control' => 'max-age=0',
                    'TE' => 'Trailers',
                ],
            ]);

$response = $client->request('GET', $url);
$crawler = new Crawler($response->getBody()->getContents());
$title = $crawler->filter('#productTitle')->text('Product Title couldn\'t be extracted', true);
$price = $crawler->filter('#priceblock_ourprice')->text('Product Price couldn\'t be extracted', true);

If trying to run this request with postman the price is present.如果尝试使用 postman 运行此请求,则存在价格。

The problem was that price was not always present on the page depends on your location, after updating to Symfony 4.4 the problem was solved and it started to return Product Price couldn\'t be extracted instead of crashing.问题是页面上并不总是显示价格取决于您的位置,在更新到 Symfony 4.4 后问题得到解决,它开始返回Product Price couldn\'t be extracted而不是崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM