14

现实世界的问题:

我的应用程序托管在Heroku上,据我所知,Heroku 无法提供运行无头(无 GUI)浏览器(例如HTMLUnit )的解决方案,以便为 Googlebot生成HTML 快照以索引我的 AJAX 内容。

我提出的解决方案:

如果您还没有,我建议您阅读 Google 的Full Specification for Making AJAX Applications Crawlable

想象一下我有:

  • 托管在域上 Heroku 上的Sinatra应用程序http://example.com
  • 该应用程序在页面顶部有选项卡TabA、TabB 和 TabC
  • 每个选项卡下是SubTab1、SubTab2、SubTab3
  • onload 如果 url 是http://example.com#!tab=TabA&subtab=SubTab3然后客户端 Javascriptlocation.hash通过 AJAX 获取并加载 TabA、SubTab3 内容。

注意:Hash Bang (#!) 是google spec的一部分。

我想构建一个托管在Google App Engine (GAE) 上的简单“网络服务”:

  1. 接受一个 URL 参数,例如http://htmlsnapshot.appspot.com?url=http://example.com#!tab=TabA&subtab=SubTab3(url 参数应该是 URLEncoded)
  2. 运行 HTMLUnit 以http://example.com#!tab=TabA&subtab=SubTab3在服务器上打开和运行客户端 JavaScript。
  3. 一旦一切都完成(或者像 45 秒过去),HTMLUnit 会返回 DOM。
  4. 返回内容可以通过 JSON/JSONP 发回,或者将 URL 返回到生成并存储在 google 应用引擎服务器上的文件(用于基于文件的“缓存”结果)......在此处接受建议。如果返回文件的 URL,那么您可以CURL来获取源代码(也称为 HTML 快照)。

我的http://example.com应用程序需要管理对http://htmlsnapshot.appspot.com... 的调用,基本上:

  1. 捕获 Googlebots 调用http://example.com/?_escaped_fragment_=tab=TabA%26subtab=SubTab3(googlebot 爬虫转义某些字符,例如 %26 = &)。
  2. 从后端发送请求到http://htmlsnapshot.appspot.com?url=http://example.com#!tab=TabA&subtab=SubTab3(url 参数应该是 URLEncoded)
  3. 将返回的 HTML Snapshot 渲染到前端。
  4. 谷歌索引内容,我们很高兴!

我没有任何使用 Google App Engine 或 Java 或 HTMLUnit 的经验。

我也许能够弄清楚......如果我这样做了,我会发布我的结果。

否则,我觉得这是一个非常好的机会,可以让某人写一篇精彩的博客文章,其中概述了新手设置此类 Web 服务的分步指南。

这将向更多人介绍优秀(而且免费!)的 Google App Engine。此外,它无疑会鼓励更多人采用 Google 的可抓取 AJAX 内容规范……我们都可以从中受益!

随着 Google 的规范获得更多接受,设置无头浏览器的“障碍”将让许多开发人员在谷歌上搜索答案!现在就加入名誉和荣耀的答案吧!(编辑:至少我会赞美你)。

@_chrisjacob如果您想讨论解决方案,请在推特上联系我。

4

1 回答 1

2

I have successfully used HTMLunit on AppEngine. My GWT code to do this is available in the gwt-platform project the results I got were similar to that of the HTMLunit-AppEngine test application by Amit Manjhi.

It should be relatively easy to use GWTP current HTMLunit support to do exactly what you describe, although you could likely do it in a simpler app. One problem I see is that AppEngine requests have a 30 second timeout, so you can't have a page that takes HTMLunit longer than that to process.

UPDATE: It's been a while, but I finally closed the long standing issue about making GWT applications crawlable using GWTP. The documentation is not entirely there, but check out the issue: http://code.google.com/p/gwt-platform/issues/detail?id=1

于 2010-08-19T02:02:39.260 回答