使file_get_contents()等待网站完全加载 [英] Make file_get_contents() wait for website to load completely

查看:233
本文介绍了使file_get_contents()等待网站完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将该网站添加到我的php变量中:

I'm trying to get this website into my php variable:

https://www.warcraftlogs.com /rankings/server/393/latest/#class = Druid& spec = Feral

您可以看到,只有在页面首次完成加载后,网站才开始加载网站的真实内容.

You can see the website is only starting to load the real contents of the website once the page has finished loading for the first time.

file_get_contents("https://www.warcraftlogs.com/rankings/server/393/latest/#class=Druid&spec=Feral");

仅返回没有库存的网站,而没有第二步中加载的表中的实际内容.

returns only the stock empty website without the actual contents from the tables that loaded in the second step.

是否有一种方法可以使file_get_contents等待网站加载?

Is there a way to make file_get_contents wait for the site to load?

推荐答案

为了了解网站上正在发生的事情,请尝试打开浏览器的网络检查器.您将看到页面本身已加载,然后将看到其他各种资源加载,例如CSS文件,JS文件,图像以及更多页面.

In order to understand what's happening on the site, try opening your browser's network inspector. You'll see the page itself load, and then you'll see various other resources load, like CSS files, JS files, images, and some more pages.

其他页面之一是:主站点似乎发出AJAX请求,以从该URL提取其他数据.请注意,file_get_contents()无法一次获取所有内容,因为file_get_contents()不会解析网站或评估任何JS(并且JS触发AJAX请求).解决方案很简单-与其使用file_get_contents()来获取主站点,不如使用它来获取包含数据的第二页面.

It looks like the main site issues an AJAX request to fetch the additional data from that URL. Note that there's no way for file_get_contents() to get everything all at once, since file_get_contents() will not parse the website or evaluate any JS (and JS is what triggers the AJAX request). The solution is simple - instead of using file_get_contents() to grab the main site, use it to grab that secondary page with the data.

如果您尝试获取此URL,则必须进行更深入的研究.如果打开主页,则会在页面上找到一段嵌入的JS,如下所示:

If you're trying to grab this URL, you'll have to dig deeper. If you open the main page, you'll find a piece of JS embedded on the page that looks like this:

function loadTable()
{
    var loadString = '/rankings/table/' + filterMetric + '/' + zoneID + '/' + filterBoss + '/' + filterDifficulty + '/' + filterSize + '/' + filterRegion + '/' + filterClass + '/' + filterSpec + '/' + filterBracket + '/' + filterServer + '/' + '?' + "search=" + filterSearch + "&page=" + filterPage
    $("#table-container").load(loadString, tableLoaded)
}

注意如何动态创建具有所需参数的字符串.然后,它调用$ .fn.load(),这会触发对URL的AJAX请求.

Notice how it's dynamically creating a string with the desired parameters. Then it calls $.fn.load(), which triggers the AJAX request to the URL.

这篇关于使file_get_contents()等待网站完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆