屏幕抓取JS页面

Cal*_*ert 3 php parsing dom

我正试图抓住这个页面http://www.buddytv.com/trivia/game-of-thrones-trivia.aspx并且它无法正常工作.

我试过了

$html = new simple_html_dom();
  $html->load_file($url);
Run Code Online (Sandbox Code Playgroud)

但是对于我想要抓住的问题(.trivia-question)是找不到的.谁能告诉我我做错了什么?

非常感谢!

我试过了

  <?php
  $Page = file_get_contents('http://www.buddytv.com/trivia/game-of-thrones-trivia.aspx');
  $dom_document = new DOMDocument();
  //errors suppress because it is throwing errors due to mismatched html tags
  @$dom_document->loadHTML($Page);
  $dom_xpath_admin = new DOMXpath($dom_document_admin);
  $elements = $dom_xpath->query('//*[@id="id60questionText"]');
  var_dump($elements);
Run Code Online (Sandbox Code Playgroud)

Aur*_*kus 5

好的,这里是phantomjs的例子:

你需要从http://phantomjs.org/下载phantomjs ,放在你可以通过脚本轻松访问的地方.

通过运行{installationdir}/bin/phantomjs(windows上的phantomjs.exe)--version来测试它

然后在项目的某处创建JS文件,ex browser.js

var page = require('webpage').create();

page.open('http://www.buddytv.com/trivia/game-of-thrones-trivia.aspx', function() {

page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {

    search = page.evaluate(function() { 
        return  $('#id60questionText').text();
    });

    console.log(search);

    phantom.exit()
  });
})
Run Code Online (Sandbox Code Playgroud)

然后在你的PHP脚本中读取它:

$pathToPhatomJs = '/home/aurimas/Downloads/phantomjs/phantomjs-1.9.1-linux-x86_64/bin/phantomjs';

$pathToJsScript = '/home/aurimas/Downloads/phantomjs/phantomjs-1.9.1-linux-x86_64/browser.js';

$stdOut = exec(sprintf('%s %s', $pathToPhatomJs,  $pathToJsScript), $out);

echo $stdOut;
Run Code Online (Sandbox Code Playgroud)

更改$pathToPhatomJs$pathToJsScript根据您的配置.

如果你在Windows上,这可能无法正常工作.然后,您可以将PHP脚本更改为:

$pathToPhatomJs = '/home/aurimas/Downloads/phantomjs/phantomjs-1.9.1-linux-x86_64/bin/phantomjs';

$pathToJsScript = '/home/aurimas/Downloads/phantomjs/phantomjs-1.9.1-linux-x86_64/browser.js';

exec(sprintf('%s %s > phatom.txt', $pathToPhatomJs,  $pathToJsScript), $out);

$fileContents = file_get_contents('phatom.txt');

echo $fileContents;
Run Code Online (Sandbox Code Playgroud)