【问题标题】:Get the content (text) of an URL after Javascript has run with PHP使用 PHP 运行 Javascript 后获取 URL 的内容(文本)
【发布时间】:2015-04-14 20:14:23
【问题描述】:

是否可以使用 PHP 获取 URL 的内容(使用某种函数,如 file_get_contentsheader),但只能在执行一些 JavaScript 代码之后?

例子:

mysite.com 有一个执行loadUrlAfterJavascriptExec('http://exampletogetcontent.com/') 并打印/回显内容的脚本。想象一些 jQuery 在 http://exampletogetcontent.com/ 上运行,这会改变 DOM,loadUrlAfterJavascriptExec 会得到结果 HTML

我们可以这样做吗?

明确一点,我想要的是通过 URL 获取页面的内容,但前提是 JavaScript 在目标页面上运行(PHP 正在获取其内容)。

我知道 PHP 在页面发送到客户端之前运行,而 JS 仅在此之后运行,但我认为可能有专家解决方法。

【问题讨论】:

  • no :-) 您希望浏览器获取页面并运行该页面的所有 js 文件并在执行后获取页面?但是您无法控制该页面?那么答案是否定的,你不能
  • 请求的url是同一个域吗?
  • @Joelerr 实际上是 Joelerr

标签: javascript php jquery curl http-headers


【解决方案1】:

更新 2 添加更多关于如何在 PHP 中使用 phantomjs 的详细信息。

Update 1(在说明 target 页面上的 javascript 需要先运行之后)

方法一:使用phantomjs(会执行javascript);

1. 下载 phantomjs 并将可执行文件放在 PHP 二进制文件可以访问的路径中。

2.将以下2个文件放在同一目录下:

get-website.php

<?php
    
    $phantom_script= dirname(__FILE__). '/get-website.js'; 


    $response =  exec ('phantomjs ' . $phantom_script);

    echo  htmlspecialchars($response);
    ?>

get-website.js

var webPage = require('webpage');
var page = webPage.create();

page.open('http://google.com/', function(status) {
 console.log(page.content);
  phantom.exit();
});

3. 浏览到get-website.php 和目标站点,http://google.com 内容将在执行内联javascript 后返回。你也可以使用php /path/to/get-website.php从命令行调用它。

方法2:使用Ajax和PHP(没有pha​​ntomjs所以不会运行javascript);

/get-website.php

<?php
    
    $html=file_get_contents('http://google.com');
    echo $html;
    ?>

test.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>on demo</title>
<style>
p {
color: red;
}
span {
color: blue;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<button id='click_me'>Click me</button>
<span style="display:none;"></span>
<script>

$( "#click_me" ).click(function () {
    $.get("/get-website.php", function(data) {
        var json = {
            html: JSON.stringify(data),
            delay: 1
        };
        alert(json.html);
        });
});
</script>
</body>
</html>

【讨论】:

  • @victor-ferreira 你有机会看看这个解决方案吗?
  • 这已过时,PhantomJS 不再生产。
【解决方案2】:

我找到了一个很棒的页面,它是关于如何在 PHP 中处理页面的 DOM 的完整教程,它完全是使用 javascript 创建的。

https://www.jacobward.co.uk/using-php-to-scrape-javascript-jquery-json-websites/ “PhantomJS 开发暂停,直至另行通知”,因此该选项不是一个好选项。

【讨论】:

  • 该文章似乎不再可用,但它可以在 waybackmachine 上找到
【解决方案3】:

我认为最简单和最好的方法是使用这个包 https://github.com/spatie/browsershot 只需完全安装并使用以下代码

Browsershot::url('https://example.com')->bodyHtml()

【讨论】:

    【解决方案4】:

    所有 PHP 在信息发送到客户端之前运行。 所有的 JavaScript 在信息发送到客户端后运行。

    要在页面加载后使用 PHP 执行某些操作,页面需要

    1. 重新加载,将 JavaScript 生成的信息保存在 cookie 中或作为 POST 数据(不理想)或
    2. 对另一个 PHP 文件进行 Ajax 调用以获取数据。 (好多了)

    由于数据似乎与您的 PHP 位于不同的文件中,因此这是一个非常好的解决方案。由于您将其标记为 jQuery,因此我假设您正在使用它。

    jQuery has a set of pages about how it implements Ajax

    但是为此使用 jQuery 最简单的方法是 .post

    例如:

    $.post( "http://example.com/myDataFile.txt", function( data ) {
        //do more JavaScript stuff with the data you just retrieved
    });
    

    $.post(),顾名思义,可以与对数据文件的请求一起发送数据,因此如果该请求是针对 PHP 文件的,那么 PHP 文件可以使用该数据。

    例如:

    $.post( "http://example.com/myDataFile.txt",
        { foo: "bar"; yabba: "dabba" },
        function( data ) {
           //do more JavaScript stuff with the data you just retrieved
    });
    

    数据应采用 JSON 格式的键/值对。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-02
      • 2013-01-18
      • 2011-04-05
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      相关资源
      最近更新 更多