获取AngularJS中指定的url的html内容 [英] getting the html content of the url specified in AngularJS

查看:156
本文介绍了获取AngularJS中指定的url的html内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用$ http.get('url')来获取url中的内容。 'url'中的html如下:

I am using the $http.get('url') to get the content present in the 'url' present. The html in 'url' is as follows

<html>
<head></head>
<body>
    <pre style = "word-wrap: break-word; white-space: pre-wrap;">
        <!-- content is present here -->
    </pre>
</body>
</html>

当我为此网址创建$ http.get时,我会获得一些数据和所需内容

When I make a $http.get to this url, I get some data and the required content

GET /TYPE=abvd HTTP 1.1
content required

.0.1234.123 Safari /123.12
Referer: //url of the webpage calling this function
Accept-Encoding: 
...............

如何摆脱这些额外信息并仅收到内容? (我知道我们可以解析响应但有更好的方法吗?)

How do I get rid of this extra information and receive only the content? (I know we can just parse the response but is there a better way of doing it? )

编辑:
我得到的数据响应是在谷歌中看到的chrome,当我在IE10中运行相同的脚本时,我得到了所需的唯一html内容。为什么会出现这种差异?如何处理呢?

The data response I got was seen in google chrome, when I run the same script in IE10, I get the only the "html content" as desired. Why does this difference occur and how do I make process it?

推荐答案

$ http.get 返回 HttpPromise ,并从中获取可以得到这样的基础数据:

$http.get returns an HttpPromise, and from it you can get the underlying data like so:

$http.get('url').then(function(response) {
    html = response.data;
});

要获得更多有用的数据,您可以像这样展开:

To get even more useful data, you can expand this like so:

$http.get('url').then(
   // success handler
   function(response) {
       var data = response.data,
           status = response.status,
           header = response.header,
           config = response.config;
   }, 
   // error handler
   function(response) {
       var data = response.data,
           status = response.status,
           header = response.header,
           config = response.config;
   });

演示: JSBin

编辑:如果还有HTML问题,您可以查看 $ sce.trustAsHtml(html)或PhantomJS并附带参考:

If there are still HTML issues, you can look into $sce.trustAsHtml(html) or PhantomJS with references:

  • Parse an HTML document with AngularJS
  • Scraping an AngularJS application

这篇关于获取AngularJS中指定的url的html内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆