简体   繁体   English

如何从GOOGLE AJAX Feed API获取新闻项目的描述

[英]How to get the description of a news Item from GOOGLE AJAX Feed API

I am using a script to load news from different sources, using Google AJAX feed API. 我正在使用脚本来使用Google AJAX Feed API加载来自不同来源的新闻。 How can I get the description of an entry? 如何获得条目的描述? Below is an hello world program: 以下是一个hello world程序:

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

    google.load("feeds", "1");

    function initialize() {
      var feed = new google.feeds.Feed("http://news.google.com/?output=rss");
      feed.load(function(result) {
        if (!result.error) {
          var container = document.getElementById("feed");
          for (var i = 0; i < result.feed.entries.length; i++) {
            var entry = result.feed.entries[i];
            var div = document.createElement("div");
            div.appendChild(document.createTextNode(entry.title));
            container.appendChild(div);
          }
        }
      });
    }
    google.setOnLoadCallback(initialize);

    </script>
  </head>
  <body>
    <div id="feed"></div>
  </body>
</html>

How can I get the description using the entry object??? 如何使用entry对象获取描述? I am using the google URL - http://news.google.com/?output=rss for RSS feeds in XML format. 我正在使用Google网址 - http://news.google.com/?output=rss获取XML格式的RSS Feed。 I want the "Description" part. 我想要“描述”部分。 How can I get that 我怎么能得到它

You can get the description, but you can't use the JSON format and the entry object to do it. 您可以获取描述,但不能使用JSON格式和entry对象来执行此操作。 If you read the feed parameters at https://developers.google.com/feed/v1/devguide carefully, you'll see that description is not a field it returns at the entry level - just at the feed level. 如果您仔细阅读https://developers.google.com/feed/v1/devguide上的Feed参数,您会看到该description不是它在入门级别返回的字段 - 仅在Feed级别。

To do it, you need to request the feed in XML format, and then load the individual nodes, including description . 为此,您需要以XML格式请求Feed,然后加载各个节点,包括description Here's the relevant snippet I've used to do it - change the formatting etc. as you need. 这是我用过的相关代码片段 - 根据需要更改格式等。

function initialize() {
   var feed = new google.feeds.Feed("http://myblog.com/blog/feed/");
   feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
   feed.load(function(result) {
   if (!result.error) {
    var items = result.xmlDocument.getElementsByTagName('item');
    item = items[0];

    //build each element
    var title = document.createElement("h4");
    title.innerHTML = item.getElementsByTagName('title')[0].firstChild.nodeValue;

    var content = document.createElement("p");
    content.innerHTML = item.getElementsByTagName('description')[0].firstChild.nodeValue;

    href = item.getElementsByTagName('link')[0].firstChild.nodeValue;
   }

The HTML description can be retrieved by using the content variable. 可以使用content变量检索HTML描述。 Thus you should have: div.appendChild(document.createTextNode(entry.content)); 因此你应该有:div.appendChild(document.createTextNode(entry.content));

Be aware that this will retrieve HTML data format. 请注意,这将检索HTML数据格式。

After much digging, I found that the Google API uses "contentSnippet" instead of description. 经过大量挖掘,我发现Google API使用“contentSnippet”而不是描述。 No XML formatting needed. 不需要XML格式。

function initialize() {
    var feed = new google.feeds.Feed("http://myblog.com/blog/feed/");   
    feed.setNumEntries(10);
    feed.load(function(result) {
        if (!result.error) {

            $(document).ready(function(){   
                $('#feed-pull').append('<ul></ul>'); 
                for (var i = 0; i < result.feed.entries.length; i++) {
                    var entry = result.feed.entries[i];                     
                    var desc = entry.contentSnippet;

Change entry.title in: 更改entry.title in:

div.appendChild(document.createTextNode(entry.title));

to entry.description. 进入。描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM