TIdHTTP 组件可以处理 javascript 代码吗?
我正在使用 TIdHTTP 组件来获取网页。对于主页来说效果很好。但它不会检索嵌入式 javascript 代码生成的内容。一个很好的例子是允许用户通过 disqus 添加评论的页面。
以下是场景中的 TIdHTTP.Get 方法不会将评论放在页面底部。
无论如何,这可以通过 Indy 组件或任何其他本机组件来完成吗?
我尝试过使用TWebBrowser OLE 控件。但我更喜欢使用本地delphi代码。
I am using the TIdHTTP component to fetch web pages. Works fine for the main page. But it does not retrieve content generated by embedded javascript code. A good example are the pages which allow users to add comments via disqus.
Here is an example
In the scenario the TIdHTTP.Get method does not get the comments down on the bottom of the page.
Is there anyway this could be done with the Indy component or any other native component?
I have experimented using TWebBrowser OLE control. But I prefer to use native delphi code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IdHTTP 不会执行 JavaScript,因为 IdHTTP 不是浏览器。您需要向应用程序引入 JavaScript 执行器,以便像浏览器一样从检索到的 HTML 源执行脚本。
您的示例是关于显示谷歌分析统计数据...这是您想要在应用程序中执行的操作吗?如果是这样,那么您尝试使用预渲染结果将其组合在一起是愚蠢的(无意冒犯)。
Google Analytics 专门提供了一个 API,以便您可以使用 HTTP 调用获取信息。一旦检索到该信息,您就可以使用本机 Delphi 组件和代码以您想要的任何富有想象力或原创的方式显示它。
由于 GA 提供了 API,因此没有任何理由按照您似乎尝试的方式进行操作。
IdHTTP will not execute JavaScripts, as IdHTTP is NOT a browser. You would need to introduce a JavaScript executor to your application to execute the scripts from the retrieved HTML source as a browser would.
Your example is about displaying google analytics stats... is this what you're trying to do in your application? If so, you're being foolish (without meaning to be offensive) trying to hack it together using a pre-rendered result.
Google Analytics provides an API specifically so you can harvest information using HTTP calls. Once that information is retrieved, you can then display it using native Delphi components and code in any imaginative or original way you desire.
Because GA provides an API, there's no excusable reason to do it the way you appear to be attempting.
不,这当然行不通。
Get
函数只是获取 Web 服务器返回的 (HTML) 文本。它甚至不知道返回的文本类型。它可以是 HTML 页面、纯文本文件或一些完全未知的字节序列。因此,对于 HTML 页面,您获得的只是纯 HTML 源,包括任何客户端脚本。事实上,JavaScript 只是嵌入在标签内的 HTML 代码中的文本内容。由您来执行脚本,就像 Web 浏览器下载 HTML 代码后执行的操作一样!
No, of course this doesn't work. The
Get
function simply obtains the (HTML) text returned by the web server. It doesn't even know what type of text that is returned. It could be a HTML page, a plain-text file, or some completely unknown sequence of bytes. In the case of a HTML page, therefore, all you get is the plain HTML source, including any client-side scripts. Indeed, the JavaScripts are merely textual content embedded in the HTML code inside<script>
tags. It is up to you to execute the script, like a web browser does after it has downloaded the HTML code!