如何在php中获取部分从jquery加载的页面的整个html [英] How to get, in php, the entire html of a page loaded in part from jquery

查看:91
本文介绍了如何在php中获取部分从jquery加载的页面的整个html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题几天...
我必须从PHP加载一个页面的整个HTML。
在这个页面上有一个jquery函数,当所有的页面被加载时被调用。该函数将其他html加载到页面中,因此我必须获取所有加载的html(使用jquery加载的部分)。我可以知道,我得到所有页面试图找到一些标签只从jquery加载。 (例如:名称为XXX的标签输入,带有属性倍数的标签输入等)

所以我试试:

  $ html = file_get_contents(http://wwww.siteToScrape.com); 
if(strpos($ html,'multiple')!== false){
echo'found';
} else {
echo'not found';
}

但结果为'未找到'。

然后我下载了简单的html dom ,然后我试试:

  include'simple_html_dom.php'; 
$ html = file_get_html(http://wwww.siteToScrape.com);
if(strpos($ html,'multiple')!== false){
echo'found';
} else {
echo'not found';
}

但结果仍然是'找不到'。

所以我想得到一些PHP脚本是什么模拟浏览器(所以也可以加载jquery),我下载了 PHP脚本Web浏览器,我试试:

  require_once('browser.php'); 
$ browser = new SimpleBrowser();
$ p = $ browser-> get('http://wwww.siteToScrape.com');
if(strpos($ p,'multiple')!== false){
echo'found';
} else {
echo'not found';
}

但结果仍然是'未找到'。
我不知道该怎么做..有人可以帮我吗?谢谢!!!!

解决方案

问题是您正在尝试混合使用服务器和客户端。



PHP在服务器上运行
在客户端浏览器中运行Javascript(并且因此也包含jQuery)。



没有简单的方法使用PHP运行JavaScript。据我所知,这是不可能的。其他语言,如Java可能能够做你正在做的事情。



你应该看看另一种方式来做到这一点。



这也是网络抓取器永远不会受到使用JavaScript的东西影响的原因。在开发时要记住这是一件好事。您的动态加载不会被这些抓取工具编入索引。


i've this problem for days... I have to load from php the entire html of a page. On this page there is a jquery function that is called when all the page is loaded. This function loads other html into page, so i have to get all the html loaded ( the part loaded with jquery too). I can know that i get all the page trying to find some tag loaded only from jquery. ( for example: tag input with name XXX, tag input with attribute multiple, etc. )

so i try:

$html = file_get_contents("http://wwww.siteToScrape.com");
if (strpos($html, 'multiple') !== false) {
    echo 'found';
} else {
    echo 'not found';
}

but result is 'not found'.

Then i downloaded simple html dom and i try:

include 'simple_html_dom.php';
$html = file_get_html("http://wwww.siteToScrape.com");
if (strpos($html, 'multiple') !== false) {
    echo 'found';
} else {
    echo 'not found';
}

but result still remain 'not found'.

so i think to get some php script what emulate browser ( so can load jquery too ) and i downloaded PHP Scriptable Web Browser and i try:

require_once('browser.php');
$browser = new SimpleBrowser();
$p = $browser->get('http://wwww.siteToScrape.com');
if (strpos($p, 'multiple') !== false) {
    echo 'found';
} else {
    echo 'not found';
}

but result is still again 'not found'. I don't know how to do it.. can someone help me??? thanks!!!!

解决方案

The problem is that you are trying to mix server and client.

PHP runs on the server Javascript (and therefor also jQuery) runs in the client browser.

There's no easy way to run the javascript using PHP. As far as I know, it's not even possible. Other languages, such as Java might be able to do what you are trying to do.

You should look at another way to do this.

This is also the reason why webcrawlers never gets affected by stuff you do using javascript. This is a nice thing to keep in mind when developing. Your dynamic loading will not be indexed by these crawlers at all.

这篇关于如何在php中获取部分从jquery加载的页面的整个html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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