我正在尝试抓取此页面 http://www.buddytv.com/trivia/game-of-thrones-trivia.aspx而且它不起作用。
我试过了
$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:http://phantomjs.org/ , 放在您可以通过脚本轻松访问的地方。
通过运行 {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()
});
})
然后在您的 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
和 $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;
关于php - 屏幕抓取JS页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17448439/