Php 屏幕抓取JS页面
我想把这一页刮下来,但它不起作用 我试过了Php 屏幕抓取JS页面,php,parsing,dom,Php,Parsing,Dom,我想把这一页刮下来,但它不起作用 我试过了 $html = new simple_html_dom(); $html->load_file($url); 但对于我想抓住的问题,找不到(.trivia question)。谁能告诉我我做错了什么 非常感谢 我试过了 <?php $Page = file_get_contents('http://www.buddytv.com/trivia/game-of-thrones-trivia.aspx'); $dom_docu
$html = new simple_html_dom();
$html->load_file($url);
但对于我想抓住的问题,找不到(.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);
好,下面是phantomjs示例:
您需要从以下位置下载phantomjs:,放在您可以通过脚本轻松访问的位置
通过运行{installationdir}/bin/phantomjs(windows上的phantomjs.exe)——版本来测试它
然后在项目中的某个地方创建JS文件,例如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()
});
})
然后在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;
根据您的配置更改$pathToPhatomJs
和$pathtojscript
如果您使用的是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;
我不明白。第三季大结局中的琐事问题课,当萨姆维尔和吉利越过墙时,他们遇到了谁?好的,但当你查看源代码时,你不会看到它,因为它是由javascript呈现的,你只会在调试器中看到它,对吗?我想这就是问题所在,是的。但是一定有一种方法可以在页面的JS被呈现后抓取页面的内容,对吗?是的,有,使用;)非常感谢。我要试着弄清楚,因为我不幸在Windows上,所以最后一部分有问题。出了什么问题?你能告诉我你是如何实现的吗?我只是在本地运行它。因此,在wamp上,我尝试添加一个带有phantom和php文件的文件夹,代码是空的,但是var是空的。您的路径错误$pathToPhatomJs
,$pathToJsScript
在您的示例中,第一个应该是phatomjs可执行文件的完整路径,第二个应该是js文件的完整路径,而不是相对路径。