xpath 从 iframe domXPath php 获取数据

我正在玩抓取网站技术,对于前链接,它总是返回空描述。原因是它由 JS 填充了以下代码,我们如何处理这些类型的 senarios。


// Frontend JS

P.when('DynamicIframe').execute(function(DynamicIframe){

    var BookDescriptionIframe = null,

        bookDescEncodedData = "book desc data",

        bookDescriptionAvailableHeight,

        minBookDescriptionInitialHeight = 112,

        options = {},

        iframeId = "bookDesc_iframe";

我正在使用 php domxpath 如下


    $file = 'sample.html';

    $dom = new DOMDocument();

    $dom->preserveWhiteSpace = false;

    // I am saving the returned html to a file and reading the file.

    @$dom->loadHTMLFile($file);

    $xpath = new DOMXPath($dom);


    // This xpath works on chrome console, but not here

    // because the content is dynamically created via js

    $desc  = $xpath->query('//*[@id="bookDesc_iframe"]')


qq_遁去的一_1
浏览 186回答 1
1回答

隔江千里

每次当你看到这些 JavaScript 生成的内容,尤其是像亚马逊、谷歌这样的大人物时,你应该立即想到它会有一个优雅的降级实现。这意味着它会在 Javascript 不能像链接浏览器那样工作的地方完成,以获得更好的浏览器覆盖率。寻找<noscript>你可能会找到一个。这样你就可以解决问题了。
打开App,查看更多内容
随时随地看视频慕课网APP