我正在尝试抓取网站的整个部分,但问题是我需要的数据从一开始就不存在。无论如何用PHP从网站上获取数据?
这是链接:https://www.iamsterdam.com/nl/uit-in-amsterdam/uit/agenda,这是我需要的部分:
在我的帖子设置为复制后,我尝试了这个 https://stackoverflow.com/a/28506533/7007968但是也没有用,所以我需要一个其他的解决方案,这就是我尝试过的:
GET-website.php
$phantom_script= 'get-website.js';
$response = exec ('phantomjs ' . $phantom_script);
echo $response;
GET-website.js
var webPage = require('webpage');
var page = webPage.create();
page.open('https://www.iamsterdam.com/nl/uit-in-amsterdam/uit', function(status) {
console.log(page.content);
phantom.exit();
});
这是我回来的全部(约占页面的3%):
</div><div id="ads"></div><script src="https://analytics.twitter.com/i/adsct?p_id=Twitter&p_user_id=0&txn_id=nvk6a&events=%5B%5B%22pageview%22%2Cnull%5D%5D&tw_sale_amount=0&tw_order_quantity=0&tw_iframe_status=0&tpx_cb=twttr.conversion.loadPixels" type="text/javascript"></script></body></html>
所以我觉得我越来越接近这就是我经过大量搜索之后的事情:
var webPage = require('webpage');
var page = webPage.create();
var settings = {
operation: "POST",
encoding: "utf8",
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify({
DateFilter: 04112016,
LastMinuteTickets: 0,
PageId: "3418a37d-b907-4c80-9d67-9fec68d96568",
Skip: 0,
Take: 12,
ViewMode: 1
})
};
page.open('https://www.iamsterdam.com/api/AgendaApi/', settings, function(status) {
console.log(page.content);
phantom.exit();
});
但我得到的回报并不好看:
Message":"An error has occurred.","ExceptionMessage":"Page could not be found","ExceptionType":"System.ApplicationException","StackTrace":" at Axendo.SC.AM.Iamsterdam.Controllers.Api.AgendaApiController.GetResultsInternal(RequestModel requestModel)\r\n at lambda_method(Closure , Object , Object[] )\r\n
等
我希望somewann可以帮助我,
答案 0 :(得分:1)
解决您的主要问题约3%。
您错误地使用exec
。当像这样使用时
$response = exec ('phantomjs ' . $phantom_script);
在执行给定命令期间, $ response将包含在终端中打印的内容的最后一行。因为你做了console.log(page.contents);
,所以HTML文档的最后一行被放入$ response变量。
正确使用exec将是
exec ('phantomjs ' . $phantom_script, $response);
这样结果将作为数组放入$ response变量中,每行都是数组的一个元素。然后,如果你只想获得html,你可以做
$html = implode("\n", $response);
但更简单和正确的方法是使用特定功能执行任务:
passthru ('phantomjs ' . $phantom_script);
passthru执行一个函数并将未经修改的接收数据直接返回到输出。
因此,如果要将其包含在变量中,请执行:
ob_start();
passthru ('phantomjs ' . $phantom_script);
$html = ob_get_clean();