【问题标题】:curl: How to fix "Please turn JavaScript on and reload the page"curl:如何修复“请打开 JavaScript 并重新加载页面”
【发布时间】:2018-07-01 11:13:26
【问题描述】:

当我使用curl 来检索 html 页面时,我遇到以下消息:

Please turn JavaScript on and reload the page

我不知道如何处理这个问题,因此我可以在我的网络浏览器上打开相同的页面。

[Q] 我怎样才能解决这个问题,以便仅使用终端检索 html 页面的信息?

$ curl http://bsod.pw/

<html>
  <head>
     <script src="https://www.google.com/recaptcha/api.js" async defer></script>
     <script>
       function onSubmit(token) {
         document.getElementById("recaptcha-form").submit();
       }
     </script>
  </head>
  <body>
<div id="recaptcha-loading" style="margin: 0px; padding: 0px; position: fixed; right: 0px; top: 0px; width: 100%; height: 100%;  z-index: 30001; opacity: 0.8;">
<p style="position: absolute; color: White; top: 30%; left: 40%;">
<img src="https://250410.selcdn.ru/antiddos/lg.rotating-balls-spinner.gif">
</p>
</div>
  <center><noscript><h1 style="text-align:center;color:red;"><strong>Please turn JavaScript on and reload the page.</strong></h1></noscript>
    <form id='recaptcha-form' action="/captcha" method="POST">
      <button id='submitbutton' style="visibility:hidden;" class="g-recaptcha" data-badge=bottomright data-sitekey="6LcigjgUAAAAACyu9edrmWKmIce8h0kIFQz7iyRo" data-callback='onSubmit'></button>
        <script>
        window.onload = function(){
        document.getElementById('submitbutton').click();
                }
        </script>
      <br/>
    </form>
    </center>
  </body>
</html>

如果您在网站上执行inspect element(http://bsod.pw/),您可以看到更详细的 html 代码。

感谢您宝贵的时间和帮助。

【问题讨论】:

标签: javascript html curl


【解决方案1】:

没有“错误”。您使用 curl 发出 GET 请求。它会返回一些 HTML。 HTML 恰好包含大部分指向您的浏览器应该加载和执行的 JavaScript 代码的链接。您的浏览器(激活 JS)可以加载 .js 脚本并运行它们。这些脚本会生成一些整洁的网页。如果您不加载链接脚本并且不执行它们,那么您不会从页面中获得太多。考虑改用合适的无头浏览器(参见下面的示例)。

这里有一个小例子可以说明这一点:

<!DOCTYPE html>
<html>
  <head>
    <title>Source code empty, page full!</title>
  </head>
  <body>
    <div id="fillThis">
      <p>Almost nothing there in the source code!</p>
      <p>... but inspect this div after JS is executed.</p>
    </div>
    <script>
      var fillThis = document.getElementById("fillThis");
      for (i = 0; i<1000; i++) {
        var child = document.createElement('p');
        child.innerHTML = "tons of content " + i;
        fillThis.appendChild(child);
      }
    </script>
  </body>
</html>    

只需将其保存为“something.html”,然后在浏览器中打开即可。当您要求浏览器显示页面源时,这正是您将得到的。但是,当您通过右键单击 div 检查它时,它会显示它附加了 >1000 个子元素。这些是由浏览器中的 JS 生成的,它们不是来自服务器的 HTML 格式。

编辑

我尝试使用 PhantomJS 访问该页面,它几乎可以正常工作。这是我所做的:

#!/bin/bash

cat <<HereDoc > /tmp/phantomjsScript.js
  var page = require('webpage').create();
  page.open('http://example.com', function(status) {
    if(status === "success") {
      console.log(page.frameContent);
    }
    phantom.exit();
  });
HereDoc

phantomjs /tmp/phantomjsScript.js

这是一个 bash 脚本,它在/tmp 中生成一个帮助脚本,然后由phantomjs 执行。 PhantomJS 加载网站,并执行 JavaScript。不幸的是,您链接到的网站受到验证码机制的保护,无法直接访问,因此上面的示例使用 example.com 代替。如果你能以某种方式解决验证码,你可能可以使用类似的脚本来加载 HTML,运行 JS,然后将渲染的 DOM 转储到控制台。

【讨论】:

  • 网站的原始输出不同。它提供了与我在问题中显示的相同的输出。但是如果你检查元素,你可以看到更多的代码。 @Dez
  • 有推荐的无头浏览器吗? @Andrey Tyukin
  • (...修补 phantomjs 解决方案。您可能想查看 PhantomJS 文档...)
  • 谢谢。你对处理 reCAPTCHA 有什么建议吗?或者我可以在不同的问题下问它吗? @Andrey Tyukin
  • 您可能应该这样做。 “为什么 curl 不给我一些 html”与“我如何用 phantomjs 重新验证”完全不同。问题主题会偏离得太远。而且我现在对recaptcha一无所知......
【解决方案2】:

尝试在 chrome 上运行代码。实际上该错误是由于验证码连接造成的,错误提示“无法联系 reCAPTCHA。请检查您的连接并重试。”

【讨论】:

  • 我需要从终端获取结果。 @MukeshR。
猜你喜欢
  • 2022-11-02
  • 2017-04-09
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多