【问题标题】:Google App Script UrlFetch not giving HTML output but JavaScript ouputGoogle App Script Url Fetch 不提供 HTML 输出,但提供 JavaScript 输出
【发布时间】:2015-07-16 10:58:10
【问题描述】:

我正在尝试从以下网页获取内容

http://www.mfinante.ro/infocodfiscal.html?cod=10376836

我的代码是

var fetchString="www.mfinante.ro/infocodfiscal.html?cod=10376836";
var response = UrlFetchApp.fetch(fetchString);

当我查看上述链接的页面源时,它显示正确的内容,但 urlfetch 显示不同的内容。

换句话说,上面页面的查看源显示html,但urlfetch只显示javascript。

查看源代码:http://www.mfinante.ro/infocodfiscal.html?cod=10376836

查看源代码有时会显示 html 有时会显示 javascript。

【问题讨论】:

  • 这里的 urlfetch 没有问题,它准确地显示了服务器响应的内容。服务器提供 javascript,浏览器执行它以构建 html 内容。因此,您在浏览器中看到的不是服务器提供的,而是副作用。
  • @Mogsdad,感谢您的评论。但请告诉我应该如何获取 HTML
  • 试试this search

标签: google-apps-script web-scraping urlfetch view-source


【解决方案1】:

使用 PhantomJS 和以下 js 代码:

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

page.open(system.args[1], function(status) {
        setTimeout(function(){
                console.log(page.plainText);
                phantom.exit();
        }, 10000);
});

在 linux 中:phantomjs 访问.js http://www.mfinante.ro/infocodfiscal.html?cod=5066472

在 Windows 中:phantomjs.exe visit.js http://www.mfinante.ro/infocodfiscal.html?cod=5066472

【讨论】:

  • 请在发布解决方案之前了解问题的上下文。这个问题与在谷歌应用程序脚本中进行网络输出抓取有关。它不在您可以使用 phantomjs 的本地计算机上。你的回答完全无关紧要。
  • 目前,Google Apps 脚本不支持模块导入。所以require方法在这里会失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 2016-01-23
  • 1970-01-01
  • 2012-01-22
相关资源
最近更新 更多