当我使用 curl
来检索 html 页面时,我遇到以下消息:
请打开 JavaScript 并重新加载页面
。
我不知道如何处理这个问题,因此我可以在我的网络浏览器上打开同一页面。
[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>
如果您在网站 ( http://bsod.pw/ ) 上执行 inspect element
操作,您可以看到更详细的 html 代码。
感谢您的宝贵时间和帮助。
最佳答案
没有“错误”。您使用curl 发出GET 请求。它会返回一些 HTML。 HTML 恰好包含大部分指向浏览器应该加载和执行的 JavaScript 代码的链接。您的浏览器(已激活 JS)可以加载 .js
脚本并运行它们。这些脚本会生成一些简洁的网页。如果您不加载链接的脚本,也不执行它们,那么您就无法从页面中获得太多信息。考虑使用适当的 headless 浏览器(参见下面的示例)。
这是一个小例子,应该可以证明这一点:
<!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 转储到控制台。
关于javascript - curl :如何修复 "Please turn JavaScript on and reload the page",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48388078/